[erlang-questions] Avoiding case nesting

Adam Kelly cthulahoops@REDACTED
Mon Sep 28 12:43:57 CEST 2009


2009/9/28 mayamatakeshi <mayamatakeshi@REDACTED>:
> case checkprefix(FirstPrefix, String) of
>    {ok, Tail} ->
>        do_this(Tail);
>    _ ->
>        case checkprefix(SecondPrefix, String) of
>            {ok, Tail} ->
> `                do_that(Tail);
>            _ ->
>                case checkprefix(ThirdPrefix, String) of
>                    and so on...
>
> So, is there a way to avoid this cascading of case expressions?

Something like:

case checkprefixes(Prefixes) of
     {ok, Tail} ->
           do_this(Tail);
     {error, not_found} ->
          ...
end.

checkprefixes([], String) ->
      {error, not_found};
checkprefixes([H|T], String) ->
      case check_prefix(H, String) of
           {ok, Tail} ->
                   {ok, Tail};
            _ ->  % I'd be specific in what I'm matching here.
                   check_prefixes(T, String)
      end.

Adam.


More information about the erlang-questions mailing list