[erlang-questions] Idiom for multiple case matches

Colm Dougan <>
Wed Nov 5 19:08:59 CET 2008


Hi,

I often find myself writing code like this :

(Note: I'm only using vowels as a contrived example here).

   case Char of
     Vowel when Vowel =:= $a;
     Vowel =:= $e;
     Vowel =:= $i;
     Vowel =:= $o;
     Vowel =:= $u ->
        io:format("Handling vowel~n");
     _ ->
        io:format("Not a vowel~n")
   end.

Is there any better idiom for this?  It usually becomes long-winded.
For example, it would seem ideal to be able to do this :

   case Char of
     $a; $e; $i; $o; $u ->
         io:format("Handling vowel~n");
    _ ->
        io:format("Not a vowel~n")
   end.

Yeah I realise I could write function either with one massive guard
statement or something like this :

handle_char($a) -> handle_vowel($a);
handle_char($e) -> handle_vowel($e);
handle_char($i) -> handle_vowel($i);
handle_char($o) -> handle_vowel($o);
handle_char($u) -> handle_vowel($u);
handle_char(Char) -> handle_non_vowel(Char).

.. which is a nice approach but sometimes an inline case statement is
all you want.

Thanks,.
Colm



More information about the erlang-questions mailing list