multiple patterns per clause?
Richard Carlsson
richardc@REDACTED
Mon Aug 28 17:18:37 CEST 2006
Yariv Sadan wrote:
> Is it possible to have a single clause match multiple patterns in Erlang?
Short answer: no.
> Specifically, I want to test if a list has a number of different
> prefixes, all of which would lead to the same expression.
> [...]
> I know the above is illegal, so what's the easiest way of doing
> something similar in Erlang?
case L of
[$f|_] -> handle(L);
[$x|_] -> handle(L);
[$a, $b, $c | _] -> handle(L);
_ ->
...
end.
handle(L) -> ...
The calls will basically be goto:s. It's not terribly verbose,
and in some cases it can even makes the code more readable.
A tip: if you have many variables to pass on, try this:
H = fun () -> handle(L, X, Y, Z, P, Q, R, S, T) end,
case L of
[$f|_] -> H();
[$x|_] -> H();
[$a, $b, $c | _] -> H();
_ ->
...
end.
/Richard
More information about the erlang-questions
mailing list