[erlang-questions] Easy way to spot undefined & empty lists?

Richard O'Keefe <>
Tue Apr 14 06:19:41 CEST 2009


On 14 Apr 2009, at 2:46 pm, Vik Olliver wrote:

> What's the nice way to do:
>
> parse_result(undefined) ->
>    {error,"String not defined"};
> parse_result("") ->
>    {error,"String not defined"}.

That is.

If you don't like the duplication,
there's a general technique ("add another function"):

parse_result(undefined) ->
     string_not_defined();
parse_result("") ->
     string_not_defined().

string_not_defined() ->
     {error, "String not defined"}.

When you are checking for patterns with no variables,
you can use == in a guard:

parse_result(Foo) when Foo == undefined ; Foo == "" ->
     {error, "String not defined"}.

This removes the duplication, but makes the patterns
harder to see.




More information about the erlang-questions mailing list