[erlang-questions] Fwd: conditional expressions

damien morton <>
Sat Nov 15 15:22:03 CET 2008


---------- Forwarded message ----------
From: damien morton <>
Date: Sun, Nov 16, 2008 at 1:21 AM
Subject: Re: [erlang-questions] conditional expressions
To: Richard Carlsson <>




On Sat, Nov 15, 2008 at 10:49 PM, Richard Carlsson <>wrote:

> damien morton wrote:
>
>> ...
>
>
>  I cant for the life of me figure out what the most concise way of stating
>> that is in erlang.
>>
>> perhaps
>>  X = if (T1=foo()) =/= [] -> T1, false -> bar() end
>>
>> it would nice to be able to say something like
>>  X = foo() otherwise bar().
>>
>
> X = case foo() of
>      [] -> bar();
>      X1 -> X1
>    end
>
> If you need to check for other values as well, replace '[] ->' with
> 'X when X =:= [] ; X =:= 0 ; ... ->'
>

Ok, that works and is reasonably concise.


>
>
> But I've always felt that this feature of Python/Perl/... boils down
> to sloppy programming style. It basically means that the caller hopes
> that the "empty or failure" case is signalled by one of the values
> reconized as pseudo-booleans by the language (the programmer might
> not actually know the exact interface of the called function, but
> guessed that this would work), and the resulting code says nothing to
> the reader about the actual set of return values. Furthermore, the
> code might do the wrong thing if the function tries to return e.g. '0'
> or '{}' on success (as opposed to False or None or whatever it usually
> uses for failure). It simply makes the code a lot less tight than it
> ought to be. And then, you still can't use the same idiom on abstract
> data types to treat e.g. an empty set as "false".


Well, Python does have a way of determining if an abstract data type is
considered true or false - there's a method the ADT can implement for that.

Still, what strikes me about the erlang libraries is the tremendous variety
of techniques used to signal the return of a value or not.

Sometimes nil/Value, sometimes false/Value, sometimes []/[Value], sometimes
false/{value,Value}

For a function that can return 0 or 1 answers, I personally like the
[]/[Value] approach.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081116/efcc71e0/attachment.html>


More information about the erlang-questions mailing list