Noob question on variable binding in a match

Interactive Elixir (1.11.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> a = 1
1
iex(2)> [a, ^a] = [2, 1]
[2, 1]
iex(3)> a 
2
iex(4)> a = 1
1
iex(5)> [^a, a] = [1, 2]
[1, 2]
iex(6)> a
2

I am not able to figure out how ’ [a, ^a] = [2, 1]’ matches. It will be very helpful if anyone can explain how the match works in this case.

When You use ^a, the variable is pinned and the system won’t try to rebind it…

iex(1)> a = 1
1
iex(2)> ^a = 2
** (MatchError) no match of right hand side value: 2
iex(3)> a = 2
2
iex(4)> ^a = 2
2

Yes, but then a has become 2, isn’t it?

Afterwards, assignment happens as a whole at once, not variable by variable. In Erlang it will look like that:

A@1 = 1,
[A@2, A@1] = [2, 1].
2 Likes

Think of it as two steps
first step: both matches are valid
second step: first a can be assigned

2 Likes

This is really helpful. Thank you.

Extending this, if the code was something like

[a, a, a, ^a] = [4, 3, 2, 1] # wont match!

will the erlang code will be like below one

[A@2, A@2, A@2, A@1] = [4, 3, 2, 1]
1 Like

Thank you.

Yes, exactly. All unbounded as need to match the same value while bounded a need to match value that was bound previously.

4 Likes

Thanks again. Got it!