从列表中获取元素的机制
是否可以在不使用函数 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用函数
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 beList.nth (a, 1)
.Note however that accessing the
n
th element of a linked list isO(n)
, so if you useList.nth
to iterate through a list, you'll end up with quadratic running time.