2
votes

I'm going through Dave Thomas Programming Elixir book, and I am confused about some of the results in the pattern matching section of the book. In particular, take the following example: [a] = [[1, 2, 3]]

My interpretation is that the match should succeed, but the value of a should be [1, 2, 3]; instead, it is [[1, 2, 3]].

Can someone help explain to me how the result came to be the value of the entire right-hand side of the match operator? Why is the output of a = [[1, 2, 3]] no different than the output of [a] = [[1, 2, 3]]?

1

1 Answers

7
votes

Actually it's different. When you pattern match

[a] = [[1, 2, 3]] 

then a became [1, 2, 3]. You are right about that. What you see as an output in iex is just the way console show it. If you check value of a it would be [1, 2, 3] as expected.

When you match

a = [[1, 2, 3]]

then a became [[1, 2, 3]]