从列表中获取元素的机制

发布于 2024-10-11 11:11:26 字数 120 浏览 8 评论 0原文

是否可以在不使用函数 head 和 tail 的情况下从新泽西州的 SML 列表中获取元素,类似这样:

val a = [1,2,3];
a[1];

提前致谢

is it possible to get element from the list in SML of New Jersey without using function head and tail, something like that:

val a = [1,2,3];
a[1];

thanks in advance

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

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

发布评论

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

评论(1

草莓味的萝莉 2024-10-18 11:11:26

您可以使用函数List.nth,它接受一个包含列表和索引的元组并返回该索引处的元素。因此,在您的示例中,它将是 List.nth (a, 1)

但请注意,访问链表的第 n 个元素的时间复杂度为 O(n),因此如果您使用 List.nth 来迭代列表,你最终会得到二次运行时间。

You can use the function List.nth, which takes a tuple containing a list and an index and returns the element at that index. So in your example, it'd be List.nth (a, 1).

Note however that accessing the nth element of a linked list is O(n), so if you use List.nth to iterate through a list, you'll end up with quadratic running time.

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