Avoiding case nesting

mayamatakeshi mayamatakeshi@REDACTED
Mon Sep 28 12:23:22 CEST 2009


Hello,
newbie question:
Suppose that I need to check if a certain string starts with one of several
prefixes.
So I write a checkprefix function that return the tail after the prefix if
it is found.
If the prefixes were known at compile time, I could use function pattern
matching and have a very clean code for this, however, the prefixes are
loaded from configuration file and I ended up writing something like this:

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?

regards,
takeshi


More information about the erlang-questions mailing list