[erlang-questions] Tuple Matching Question

Santosh Kumar <>
Tue Dec 9 19:08:46 CET 2014


Hi,
I just had a question on pattern matching in tuples. Is it possible to
perform Head and Tail extraction on a Tuple as I would on a list.

So for example, something like so:

{H|T} = {a,b,c}.

And I expect H to be {a} and T to be {b,c}. I know this works on Lists,
just trying to get it to work on Tuples.

The reason I ask is, I am working my way through Joe Armstrong's book and
one of his exercise is implementing a tuple_to_list function and my
approach was as follows:

my_tuple_to_list(T) -> my_tuple_to_list_acc(T, []).
my_tuple_to_list_acc({}, Acc)    -> Acc;
my_tuple_to_list_acc({H,T}, Acc) -> my_tuple_to_list_acc(T, [H|Acc]).

Thanks
Santosh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141209/2d61945f/attachment.html>


More information about the erlang-questions mailing list