记录列表上的SML功能

发布于 2024-12-23 19:50:30 字数 227 浏览 5 评论 0原文

我试图声明一个函数,该函数将元组内的记录列表作为参数,但语法并不像我希望的那样直观。

这就是我想做的:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坐在坟头思考人生 2024-12-30 19:50:30

模式匹配需要将记录字段绑定到某些值,因此您必须使用显式记录语法。因此,

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

会起作用。

请注意,上述模式匹配并不详尽,请注意 players 的空列表和 privateStack 的空列表:

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

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 *)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文