[erlang-questions] Avoiding case nesting

mayamatakeshi mayamatakeshi@REDACTED
Mon Sep 28 14:08:42 CEST 2009


On Mon, Sep 28, 2009 at 7:38 PM, Jayson Vantuyl <kagato@REDACTED> wrote:

> I believe that you can still use pattern matching.  Something like:
>
>  case String of
>>  [ FirstPrefix | Tail ] -> do_this(Tail);
>>  [ SecondPrefix | Tail ] -> do_that(Tail);
>>  ...
>>  _ -> didnt_match
>> end.
>>
>
Hello,
I tried like the above, but it will only work if the prefixes are single
characters like $a. If the prefix is a string, then it will not match
because it will become a list within a list.


> Although, if you have a variable number of prefixes, it may be better do
> this with recursion and a list of {Prefix,HandlerFunc}.
>

Yes, I can see this now, as all replies talk about it.


>
> Also, if you do have to work with nested cases, this article might help you
> make them easier to debug:
>
>  http://www.trapexit.org/Nested_Cases
>>
>
> Thanks


More information about the erlang-questions mailing list