开发者

mechanism to get element from the list

开发者 https://www.devze.com 2023-02-04 07:47 出处:网络
is it possible to g开发者_如何学编程et element from the list in SML of New Jersey without using function head and tail, something like that:

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

精彩评论

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