Pattern matching

The match operator

The equals sign matches the left pattern against the right value. When the left side holds names, it binds them; when it holds literals, it checks them.

This one operator is the backbone of the language: assignment, argument passing, and case branches are all matches.

erl

1> Point = {1, 2}.
{1,2}
2> {1, Y} = Point.
{1,2}
3> Y.
2

Nested patterns

Patterns nest as deep as your data: a tuple inside a list inside a map. The match succeeds only when every level lines up.

Deep patterns replace whole chains of index reads and if tests in other languages.

erl

1> {user, #{name := Who}} = {user, #{name => "Ada"}}.
{user,#{name => "Ada"}}
2> Who.
"Ada"

Wildcards

The underscore matches anything and binds nothing. It marks the parts of the data you do not need.

An underscore with a name binds the value for inspection while telling the compiler you plan to ignore it.

erl

1> {ok, _, done} = {ok, 7, done}.
{ok,7,done}
2> [_, Second | _] = [1, 2, 3].
[1,2,3]
3> Second.
2

In function heads

Function heads are patterns too. Each clause matches its arguments in order, so the first clause that fits runs with the matched parts already bound.

This is why Erlang code so often has no explicit assignment at all: the arguments arrive already unpacked.

-module(shapes). -export([area/1]). area({square, S}) -> S * S; area({rect, W, H}) -> W * H.

erlc shapes.erl
erl

1> shapes:area({square, 3}).
9
2> shapes:area({rect, 2, 3}).
6

Next: Guards

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the left side of = hold?

    Answer

    A pattern

    The match operator puts a pattern on the left and a value on the right.

  2. What does Point = {1, 2}. return?

    Answer

    {1,2}

    A successful match evaluates to the value of the right side.

  3. What does {1, Y} = {1, 2}. bind?

    Answer

    Y to 2

    The first item checks the literal 1, and the name Y receives the second item.

  4. What happens when a literal on the left does not match?

    Answer

    A badmatch exception is raised

    A pattern that cannot match the value raises an error instead of returning false.

  5. How deep can a pattern nest?

    Answer

    As deep as the data goes

    Tuples, lists and maps can hold each other, and a pattern can follow the same shape.

  6. What does the lone _ bind?

    Answer

    Nothing

    The underscore matches any value and keeps no binding.

  7. What is _Name for?

    Answer

    A binding that the compiler need not warn about

    The leading underscore keeps the value available while marking the binding as intentionally unused.

  8. Are function heads patterns?

    Answer

    Yes, the arguments are matched clause by clause

    Each clause of a function matches its arguments, so the parts arrive already bound.

  9. What does shapes:area({square, 3}). return?

    Answer

    9

    The square clause binds S to 3 and returns S * S.

  10. Why do deep patterns replace chains of index reads?

    Answer

    One pattern checks the shape and binds every part at once

    A single pattern states the expected shape and extracts the values, so no separate checks are needed.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image