[erlang-questions] Avoiding case nesting

mayamatakeshi mayamatakeshi@REDACTED
Mon Sep 28 14:13:39 CEST 2009


On Mon, Sep 28, 2009 at 7:43 PM, Adam Kelly <cthulahoops@REDACTED> wrote:

> 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.


Thanks. This worked fine.


More information about the erlang-questions mailing list