Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mechanism to get element from the list

Tags:

list

sml

smlnj

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

like image 593
rookie Avatar asked Jan 12 '11 11:01

rookie


1 Answers

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.

like image 162
sepp2k Avatar answered Sep 28 '22 07:09

sepp2k