SML function on record list

最后都变了- 提交于 2019-12-10 16:21:36

问题


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:


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


来源:https://stackoverflow.com/questions/8711264/sml-function-on-record-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!