4

I'm trying to declare a function that takes a list of records inside a tuple as an argument but the syntax is not as intuitive as I would have liked.

Here's what I'm trying to do:

type Player = {id:int, privateStack:int list};
fun foo(({id, x::xs}:Player)::players, ...) = (* wrong syntax *)
    (* do something *)
1
  • I think I just realised my mistake: apparently you have to redeclare the record labels in the argument as in {id=ID, privateStack=x::xs} otherwise I guess it treats them as constructors. Commented Jan 3, 2012 at 11:23

1 Answer 1

6

Pattern matching requires binding record fields to some values so you have to use explicit record syntax. Therefore,

fun foo(({id = id, privateStack = x::xs})::players, ...) =
    (* do something *)

would work.

Note that above pattern matching is not exhaustive, be aware of empty list for players and empty list for privateStack:

fun foo([], ...) = (* do something *)
   | foo({id = id, privateStack = []}::players, ...) = (* do something else *)
   | foo({id = id, privateStack = x::xs}::players, ...) = (* do something else *)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.