Reputation: 319
I've just started learning SML and now I wonder how you get elements from a list of tuples? Say that I have the list [(#"D", 7), (#"E", 5), (#"M", 1), (#"N", 6), (#"O", 0), (#"R", 8), (#"S", 9), (#"Y", 2)] and just want the integers so I can add them together like 7 + 5 + 1 + 6 + 0 + 8 + 9 + 2. Is this possible, and if so, how can it be done?
Upvotes: 1
Views: 2049
Reputation: 1826
The following gets the list containing all the second elements of the tuples - that is, the integers in your example list.
fun getSeconds [] = []
| getSeconds ((_,x)::xs) = x :: getSeconds xs
If you want to add them as you go along:
fun sumSeconds [] = 0
| sumSeconds ((_,x)::xs) = x + sumSeconds xs
Upvotes: 4