在 SML 中的 'a 列表上使用 List.nth

发布于 2024-09-28 07:54:25 字数 395 浏览 0 评论 0原文

我试图声明一个函数 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 技术交流群。

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

发布评论

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

评论(1

友谊不毕业 2024-10-05 07:54:25

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 function f and a list [x0, x1, ..., xn] and returns [f x0, f x1, ..., f xn].

So if we define f x to be List.nth(x,n), you get back [List.nth (x0, n), List.nth (x1, n), ..., List.nth (xn, n)], which is exactly what you want.

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