Elixir pattern matching just a little bit unaccustomed, in Elixir
which =
operators are called match operators
IEX > x = 1
1
IEX (+) > x
1
IEX (+) > 1 = x
1
IEX (+) > 2 = x
* * (Matcherror) No match of right hand side value:1
1=x legally equals 1.
An error occurs when the 2=x sides are unequal MatchError
Match list
IEX (2) > A = [1]
[1]
IEX (3) > [h|t] = a
[1]
IEX (4) > H
1
IEX (5) > t
[]
with [h|t] = A---"[h|t]=[1]--" H matches to 1 T is empty
if [h|t] = []
* * (Matcherror) No match of right hand side value: []
Matching tuples
IEX (+) > {A, B, c} = {: Hello, ' word ', 33}
{: Hello, "word", 33}
IEX (> a)
: Hello
IEX (Panax Notoginseng) > B
"Word"
IEX (> C)
33
Both sides do not match
IEX ($) > {A, B, c} = {: Hello, ' word '}
* * (Matcherror) No match of right hand side value: {: Hello, "word"}
The first one on the right is not: OK also does not match
IEX (+) > {: OK, result} = {: OK, 13}
{: OK, 13}
IEX ($) > result
13
IEX > {: OK, result} = {: Error,: oops}
* * (Matcherror) No match of right hand side value: {: Error,: oops}
Elixir Pattern Matching