rookie
rookie

Reputation: 7883

mechanism to get element from the list

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

Upvotes: 6

Views: 8978

Answers (1)

sepp2k
sepp2k

Reputation: 370397

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.

Upvotes: 9

Related Questions