Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang - parallel message is not seeking correctly

I have a little issue to which I can find no easy answer.

I set:

Who = apple.

Message = [{apple, {0,0,0}}, {orange, {1,1,1}}].

Old = [X || {Who, X} <- Message].

Old returns as [{0,0,0},{1,1,1}]

Of course my expected response was {0,0,0}

Instead I get both apple and orange.

What could I do??

like image 733
BAR Avatar asked Jan 22 '23 07:01

BAR


2 Answers

This is actually the same question as in Why Erlang variable is unused? where I gave an answer. Basically it is because all patterns variables in comprehensions are new, fresh variables so if you want to test them you need to do it explicitly. It is same as for variables occurring in the head of funs.

like image 92
rvirding Avatar answered Jan 23 '23 21:01

rvirding


Interesting question, I really liked that one! Looks like the scoping rules of Erlang are a bit odd (or at least I am not used to it, because I don't use Erlang regularly)... But the following seems to work:

[Old|_] = [X || {W, X} <- Message, W =:= Who].
like image 34
tux21b Avatar answered Jan 23 '23 21:01

tux21b