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??
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.
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].
精彩评论