在 SML 中的 'a 列表上使用 List.nth
我试图声明一个函数 takelist : 'a list list ->整数-> '一个列表,因此调用 takelist xs n 将返回 xs 列表中编号为 n 的元素。
takelist [[#"3", #"6"], [#"6", #"2"], [#"5", #"9"]] 1;
应返回 [#"6", #"2", #"9"]。
这就是我所拥有的:
fun tagliste (x::xs) n = List.nth(x,n);
我做了我想做的一半,但我不知道如何获得一切。我只是从第一个列表中获取 n,而不是全部。有人告诉我地图会有所帮助,但到目前为止我还没有正确使用它。
任何帮助表示赞赏!
I'm trying to declare a function takelist : 'a list list -> int -> 'a list, so that calling takelist xs n will return the elements in spot number n in the lists from xs.
takelist [[#"3", #"6"], [#"6", #"2"], [#"5", #"9"]] 1;
should return [#"6", #"2", #"9"].
This is what I have:
fun tagliste (x::xs) n = List.nth(x,n);
I does half of what I want, and I can't figure how to get everything. I'm just getting the n from the first list instead of all of them. I've been told that map will be able to help, but so far I haven't had any luck using it correctly.
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
map
接受函数f
和列表[x0, x1, ..., xn]
并返回[f x0, f x1, ..., f xn]
。因此,如果我们将
f x
定义为List.nth(x,n)
,您将得到[List.nth (x0, n), List.nth ( x1, n), ..., List.nth(xn, n)]
,这正是您想要的。map
takes a functionf
and a list[x0, x1, ..., xn]
and returns[f x0, f x1, ..., f xn]
.So if we define
f x
to beList.nth(x,n)
, you get back[List.nth (x0, n), List.nth (x1, n), ..., List.nth (xn, n)]
, which is exactly what you want.