Extraction of elements of tuples
Given one list with one tuple:
[{4,1,144}]
How to extract the first element of the tuple insi开发者_JAVA技巧de the list:
element(1,lists:nth(1,L))
Do you have a simpler solution?
Try this:
1> A = [{3,1,1444}].
[{3,1,1444}]
2> [{X, _, _}] = A.
[{3,1,1444}]
3> X.
3
4>
Given that you get exactly what you state, a list with one tuple, even easier would be (using element/2)
element(1, hd(L)).
A pattern matching variant like shk suggested is probably even nicer, depending on the context.
you could also consider using records syntax if you want some semantics embedded into your tuples
-record(x, {y, z}).
1> A = #x{y=b, z=c}.
2> A#x.y.
b
all records are in fact tuples and you dont have to worry about order of elements in that tuple nor about adding/removing elements.
精彩评论