Erlang - concurrent message not looking correctly

I have a small problem that I cannot find an easy answer to.

I have installed:

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, the expected response was {0,0,0}

Instead, I get both an apple and an orange.

What can I do?

+4
source share
2 answers

An interesting question, I really liked it! It seems that Erlang's review rules are a bit weird (or at least I'm not used to it because I don't use Erlang regularly) ... But it seems the following works:

 [Old|_] = [X || {W, X} <- Message, W =:= Who]. 
+4
source

This is actually the same question as in Why is the Erlang variable not used? where I gave the answer. This is mainly because all variable templates, in their understanding, are new, fresh variables, so if you want to test them, you need to do this explicitly. This is the same as for variables occurring in the heads.

+5
source

All Articles