
Learning Elixir
By :

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 2
to a
and then add 4
to 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.
In step 1
, for Elixir to make the match succeed, we bind the value of 2
to the variable, a
. Then later, when we perform the addition, we are substituting 2
for a
, yielding an expression that looks like 2 + 4, which obviously equals 6
.
This is a really different way to think about what is going on internally. Take a moment to let it sink in.
Back...
Change the font size
Change margin width
Change background colour