[erlang-questions] newbie: how to ignore the rest in a variable length tuple?

Anthony Kong anthony.hw.kong@REDACTED
Fri Feb 29 23:53:01 CET 2008

Hi, all,

What I want to achieve is this:

In the case I received a tuple like {x, B, C, D} I want to perform
some action using B, C, D.

But if I received a tuple like {y, ...} I just don't care the rest of
data in the tuple.

So, I tried a syntax {y, _}, but this led to a function_clause
exception. It is because erl applies this to a tuple of 2 elements,
not "tuple of any length with first element == y".

Is there any alternative to "{y, _, _, _} " ?

Because of the way I construct the messages, it can be a tuple of 4 or
5 elements.  That means If I have to define a clause for {y, _, _, _}
then I have to also define another one for {y, _, _, _, _}. I am
looking for a leaner expression.

Cheers, Anthony



test() ->
  R = c1({x, b, c, d}),
  io:format("~p~n", [R]),
  R1 = c1({y, b, c, d}),
  io:format("~p~n", [R1]).

c1({x, B, C, D}) ->
  io:format("~p ~p ~p~n", [B, C, D]),
  {ok, get_x};

c1({y, _}) ->
    {ok, get_y}.  %% throw function_clause exception


Don't EVER make the mistake that you can design something better than
what you get from ruthless massively parallel trial-and-error with a
feedback cycle. That's giving your intelligence _much_ too much

- Linus Torvalds

More information about the erlang-questions mailing list