[erlang-questions] Idiom for multiple case matches

karol skocik karol.skocik@REDACTED
Wed Nov 5 19:27:30 CET 2008


You might consider

case lists:member(Char, [$a, $e, $i, $o, $u]) of
    true  -> io:format("Handling vowel~n");
    false -> io:format("Not a vowel~n")
end.

a viable approach, esp. because you can extract the list inside
member() when you need it on more places.

On Wed, Nov 5, 2008 at 7:08 PM, Colm Dougan <colm.dougan@REDACTED> wrote:
> 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
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list