Elixir — A Tincture for Functional Programming Part 1.4 Pattern Matching

Kyle Ledoux
3 min readFeb 8, 2022

Pattern Matching

Intro

One concept that I found initially tricky to understand was that of pattern matching. In Elixir, I found that the humble `=` operator does much more than simple assignment. It also provides useful pattern matching functionality that is extremely helpful when destructuring parts of values. Pattern matching also plays a large role in control flow in functional languages, largely taking the place of common control flow idioms like `if/else` statements.

So, knowing that the `=` operator plays an important role in pattern matching, and further, that pattern matching plays an important role in dictating control flow in functional programming, what exactly is it?

What is pattern matching in Elixir?

Before getting into the “fun” stuff that we can do with pattern matching, it is helpful to loosely define what pattern matching is in Elixir. As Ulisses Almeida succinctly states:

The basis of pattern matching is that it tries to make two things match, and it does something when it fails to do so.

The =operator will perform a pattern match, trying to match the value of the expression on the left with the value of the expression on the right. If there is a match, then the program continues to run, if there is no match, then Elixir will raise a MatchError and halt program execution.

Variable assignment is a form of pattern matching; Elixir is making the value on the left (the variable) match the value on the right. When a variable is given on the left of the =operator, Elixir will bind it to the value on the right.

Pin it!

If you have set a variable to a certain value and do not want to reassign it, but rather try to perform a pattern match with it, you can prefix the variable name with the ^pin operator.

Destructuring

The most interesting use for pattern matching that I have encountered so far is destructuring. Using variables and pattern matching, we can extract specific elements from different types of values. Here are a few value types that I have found most helpful when destructuring.

List:

For pattern matching with Lists we use [].

The most useful form of pattern matching destructuring with lists uses the [head | tail] syntax. This will extract the first value in the list to the variable head and assign the remaining list to the variable tail.

Destructuring like this is invaluable in iterating through and transforming lists.

There are other forms of List pattern matching as well. We can use a pattern of variables to verify that the corresponding values in the list follow the same pattern. Or we can ignore parts of the list by using the _wildcard character. This will match any corresponding value in the collection.

Map:

Using pattern matching we can easily grab values from a Map and assign it to a variable while checking values for matches.

String:

Using pattern matching can be a handy trick with Strings to extract key bits from a larger value. Use the <>operator to check the beginning of a string:

Remember that the variable cannot be placed on the left of the <>operator or an error will be raised!

Further Reading

There is plenty more to learn about pattern matching including how to do so with additional value types and how to use them for control flow, among other things. If you want to learn more, I’d suggest reading the relevant sections in The Joy of Elixir and Learn Functional Programming with Elixir for some great explanations with easy-to-understand examples.

--

--

Kyle Ledoux

I’m a software engineer with a talent for distractable curiosity and a passion for questionable humor.