More Complex Matches

First, a little background syntax. Elixir lists can be created using square brackets containing a comma-separated set of values. Here are some lists:

 [ ​"​​Humperdinck"​, ​"​​Buttercup"​, ​"​​Fezzik"​ ]
 [ ​"​​milk"​, ​"​​butter"​, [ ​"​​iocane"​, 12 ] ]

Back to the match operator.

 iex>​ list = [ 1, 2, 3 ]
 [1, 2, 3]

To make the match true, Elixir bound the variable list to the list [1, 2, 3].

But let’s try something else:

 iex>​ list = [1, 2, 3]
 [1, 2, 3]
 iex>​ [a, b, c ] = list
 [1, 2, 3]
 iex>​ a
 1
 iex>​ b
 2
 iex>​ c
 3

Elixir looks for a way to make the value of the left side the same as the value of the right side. The left side is a list containing three variables, and the right is a list of three values, so the two sides could be made the same by setting the variables to the corresponding values.

Elixir calls this process pattern matching. A pattern (the left side) is matched if the values (the right side) have the same structure and if each term in the pattern can be matched to the corresponding term in the values. A literal value in the pattern matches that exact value, and a variable in the pattern matches by taking on the corresponding value.

Let’s look at a few more examples.

 iex>​ list = [1, 2, [ 3, 4, 5 ] ]
 [1, 2, [3, 4, 5]]
 iex>​ [a, b, c ] = list
 [1, 2, [3, 4, 5]]
 iex>​ a
 1
 iex>​ b
 2
 iex>​ c
 [3, 4, 5]

The value on the right side that corresponds to the term c on the left side is the sublist [3,4,5]; that is the value given to c to make the match true.

Let’s try a pattern containing some values and variables.

 iex>​ list = [1, 2, 3]
 [1, 2, 3]
 iex>​ [a, 2, b ] = list
 [1, 2, 3]
 iex>​ a
 1
 iex>​ b
 3

The literal 2 in the pattern matched the corresponding term on the right, so the match succeeds by setting the values of a and b to 1 and 3. But…

 iex>​ list = [1, 2, 3]
 [1, 2, 3]
 iex>​ [a, 1, b ] = list
 **​ (MatchError) no match of right hand side value: [1, 2, 3]

Here the 1 (the second term in the list) cannot be matched against the corresponding element on the right side, so no variables are set and the match fails. This gives us a way of matching a list that meets certain criteria—in this case a length of 3, with 1 as its second element.