extraction of elements of tuples

爱⌒轻易说出口 提交于 2019-12-21 07:06:23

问题


Given one list with one tuple:

[{4,1,144}]

How to extract the first element of the tuple:

element(1,lists:nth(1,L))

Do you have a simpler solution?


回答1:


Try this:

1> A = [{3,1,1444}].
[{3,1,1444}]
2> [{X, _, _}] = A.
[{3,1,1444}]
3> X.
3
4> 



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4819506/extraction-of-elements-of-tuples

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!