Klausos Klausos
Klausos Klausos

Reputation: 16050

SML: get element from the list

Question about Standard ML (SML). Is it possible to get an element from the list by using the row and column index? Something like this:

myList = [(1,9,3),(3,5,5),(7,0,1)]
myList[1,2] // 1st row 2nd column => in this case 9

Upvotes: 3

Views: 9168

Answers (1)

ruakh
ruakh

Reputation: 183484

I don't think it's a good idea to think in terms of "rows" and "columns", but yes, you can do what you describe, by writing this:

#2 (List.nth (myList, 0))    (* 0th element, component #2 -- in this case 9 *)

List.nth is part of the Standard ML Basis Library; it maps from 'a list * int to 'a. In this case List.nth (myList, 0) is (1,9,3). (Note that it uses zero-based indexing.) #2 is a built-in part of the language, referring to the 2-labeled component of a record (such as, as in this case, the second component of a tuple).

Upvotes: 2

Related Questions