[erlang-questions] Tried this... Thought it interesting... But it didn't work.

Richard A. O'Keefe <>
Wed Sep 2 05:37:11 CEST 2015


On 2/09/2015, at 3:30 am, <> <> wrote:

> So, I ran across this tidbit in the Erlang cybersphere:
> 
> List Comprehension without generators
> 
> http://blog.equanimity.nl/blog/2015/03/15/erlang-one-weird-trick-goodiebag/
> 
> 1> Y = 3.
> 3
> 2> Z = [X || Y > 2].
> * 1: variable 'X' is unbound

Why did you think that would work?  What did you expect it to be?

1> Y = 3, A = [a || Y < 2], B = [b || Y > 2], {A,B}.
{[],[b]}

Perhaps more interesting:

2> [io:write(a) || Y < 2], [io:write(b) || Y > 2].
b[ok]

from which we see that io:write(a) was not called,
while io:write(b) was called.

3> Pid = spawn(fun () -> receive X -> io:write('OK') end end).
<0.38.0>
4> [Pid ! a || Y < 2].
[]
5> [Pid ! b || Y > 2].
'OK'[b]

And this, boys and girls, means that Erlang has a
one-armed 'if <general expression> then <expression>'
statement.  All those years I've been saying "we don't
need no stinking COND" and we already had one...




More information about the erlang-questions mailing list