I'm new to SML, and I was wondering how to get an element in a list of tuples. For example, in the list [("abc", 4), ("def", 6)], how could you extract "abc"? I've tried
x::xs => #1(x)
but I keep getting "unresolved flex record". Any suggestions?
I'm new to SML, and I was wondering how to get an element in a list of tuples. For example, in the list [("abc", 4), ("def", 6)], how could you extract "abc"? I've tried
x::xs => #1(x)
but I keep getting "unresolved flex record". Any suggestions?
You can just extract it using pattern matching.
let
val lst = [("abc", 4), ("def", 6)]
in
case lst of (str,_)::xs => str
| [] => "The list is empty"
end
Will return "abc"
.
What you posted works too, in a case expression. I don't know what you were trying to do.
let
val lst = [("abc", 4), ("def", 6)]
in
case lst of x::xs => #1 x
| [] => "The list is empty"
end