One of the most misunderstood concepts in functional programming is that of assignment. Or, said another way, assignment doesn't exist.
Let's try to dispel this misconceived idea. In
iex, we might see some code like this:
iex(1)> a = 2 2 iex(2)> a + 4 6
We may be tempted to explain the preceding code snippet with something like, "So we assign
a and then add
a giving us
6." However, in Elixir, this is incorrect. Elixir does not define
= as an assignment operator, but rather a match operator. That is, Elixir attempts to match the left side of the
= operator to that of the right.
1, for Elixir to make the match succeed, we bind the value of
2 to the variable,
a. Then later, when we perform ...