开发者

mechanism to get element from the list

is it possible to g开发者_如何学编程et 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜