简体   繁体   中英

SML function on record list

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 *)

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 *)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM