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

Paul Fisher pfisher@REDACTED
Sat Mar 1 01:27:07 CET 2008


On Sat, 2008-03-01 at 09:53 +1100, Anthony Kong wrote:
> 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, _, _, _} " ?

Use guards:

c1( {x, B, C, D} ) -> ok;
c1( Y ) when is_tuple(Y), element(1, Y) =:= y -> ok.

if you want to restrict Y to 4 or 5 elements only, then add:

 length(Y) >= 4, length(Y) =< 5

to the list of guards.


--

paul




More information about the erlang-questions mailing list