SML function in the list of entries

I am 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 like.

Here is what I am trying to do:

type Player = {id:int, privateStack:int list}; fun foo(({id, x::xs}:Player)::players, ...) = (* wrong syntax *) (* do something *) 
+4
source share
1 answer

Matching a pattern requires binding the record fields to some values, so you need to use explicit record syntax. Hence,

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

will work.

Note that pattern matching is not exhaustive; remember the empty list for players and the 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 *) 
+6
source

Source: https://habr.com/ru/post/1389055/


All Articles