Pattern Matching in Elixir: Five Things to Remember

Posted on by in Development, Elixir

Elixir has been getting a lot of attention these days for being such a powerful language. It is highly-concurrent, fault-tolerant, and scalable.

When beginning to learn Elixir, you quickly come upon the term Pattern Matching.
What is Pattern Matching and more importantly what do you need to know?

1) It’s Not Assignment

In Elixir there is no real concept of assignment as you might be used to in other programming languages (Ruby, Javascript, Java, etc.). The “=” is a match operator. It allows us to compare the right side of an expression(known as the “pattern”) against the left side (known as the “term”) to see if they “match”.

Let’s take a look in more detail:

If the examples above were from an imperative language, we could assume that we have a variable on the left and the value on the right and an assignment operator “=.”

Given there is no assignment operator – what is happening?

When a new variable is being initialized(as seen above), the following happens:

  • The pattern on the right is evaluated (either “1” or “2”)
  • The resulting value is matched against the left-side term (“a” or “b”)
  • The variables on the left-hand side is initialized and bound to the value on the right-hand side.

In Elixir we would say “a” is bound to “1” and “b” is bound to “2”.

Let’s take a look at another example:

What is happening here? This does not look like something we would normally see in an imperative language.

Looking above we know that “b” has a value of “2”. The pattern on our right (the value of b) is being matched to the term on the left-hand side “2”.

The pattern matches and so the value on the right-hand side is returned.

Yet what happens when the pattern doesn’t match?

Remember that “a” has a value of “1”. As the pattern is evaluated, the pattern on the right does not match the term left and we receive an error.

2) Variables are Bound and Rebound

As mentioned, in Elixir we cannot assign values to variables. However, we can bind a variable to a value. If a variable already has a value, we can rebind that value.

Here “b” is being rebound from a value of “2” to a value of “3″:

The last thing to note about variables is the pin(^) operator:

When using the pin operator we are matching against the contents of the variable rather than binding the variable to the term on the right-hand side.

In this case the pattern 4 does not match the term 3 (the value of the pinned variable ^b), we receive an error.

3) Matching in data structures.

Using pattern matching in data structures is common in Elixir:

In the example below, the untyped data structure with the curly braces and containing comma-delimited elements is called a Tuple. Tuples are generally used to group a fixed number of elements.

This example assumes that the right-hand pattern is a tuple with two elements. Notice that the left hand-term is a tuple with two variables.

The element with the value “:ok” is an atom. Atoms are literal constants in Elixir. They look like symbols for those familiar with Ruby or C/C++.

What do we imagine will happen above with what we know about pattern matching?

We know that Elixir is going to match the pattern on the right to the term on the left.
In this case, the variables “result” and “value” will each be bound to their respective values on the right-hand side.

As we can see above, once the variables are bound, the pattern on the right is the return value.

If we set the first element in the term as an atom “:ok” and leave the second item, we can see that the variable “value” is bound to the respective element in the right-hand term.

What happens when the pattern does not match?

Above we can see that the first element in the tuple on the right-hand side does not match the first element in the tuple on the left – and so we receive an error.

It is a common pattern in Elixir for functions to return either { :ok, result } or { :error, reason }.

4) Multi-Clause Functions

A multi-clause function is a function that has multiple definitions with the same arity (number of arguments). Pattern matching is very powerful when used in combination with multi-clause functions.

Let’s create a “Greeter” module. In Elixir we use modules to group together functions:

We have two definitions for the function “hello”. Notice that each function accepts a different argument. The function that is executed is the one where the arguments that are passed in match the pattern of the arguments in that function’s signature.

What are the benefits here?
Pattern matching forces us to be very intentional about our data, since each function takes a specific signature.
It also allows for more atomic code – the ability to break functions into small pieces.
It prevents the need for complicated conditional logic.

5) Recursion

In Elixir we think about looping differently than we might in imperative languages like Ruby or Javascript. Looping constructs like ‘while’ or ‘do while’ are not provided. The way we accomplish looping in Elixir is through recursion.

Let’s take a look at recursion using lists.

This data structure below that looks like what might be an array in Ruby or Javascript is actually a singly linked list. Elixir allows you to reference the first element in the list as the “head” and the rest of the list as the “tail”.

Here we have the “Calculator” module:

We have two function with the same arity. One returns a list with each item in the list multiplied by 2. The other returns an empty list.

When we call our Calculator function with a list, notice that we multiply the head of the list by 2. Then we call the function “multiply_by_2” on the tail of the list. It will continue iterating through each item in the list until the list is empty.

Once the list is empty, that empty list will match against the function that takes an empty list as an argument. That function will execute and the recursion will stop. This type of recursion is called “tail recursion” and is often how we iterate over lists in Elixir.

And that’s it – the basics of pattern matching in Elixir.

In summary:

  1. It’s not assignment
  2. Variables are bound and rebound
  3. Pattern matching in data structures is common
  4. Use pattern matching with multi-clause functions
  5. Recursion allows for looping.