[erlang-questions] Avoiding case nesting

mayamatakeshi mayamatakeshi@REDACTED
Mon Sep 28 15:11:07 CEST 2009


On Mon, Sep 28, 2009 at 9:23 PM, Dale Harvey <harveyd@REDACTED> wrote:

>
>
> 2009/9/28 mayamatakeshi <mayamatakeshi@REDACTED>
>
>> 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.
>>
>>
> you can use
>
>   case String of
>     FirstPrefix ++ Tail   -> do_this(Tail);
>     SecondPrefix ++ Tail  -> do_that(Tail);
>     "a" ++ Tail           -> do_that(Tail);
>
> to match lists
>

Thanks, however, in my case, the prefixes are variables set to strings and
not literal strings, so when I try the above it will not even compile: erlc
will complain with "illegal pattern". Only the last pattern ( "a" ++ Tail )
will pass compilation.


More information about the erlang-questions mailing list