Skip to content

Erlang Pattern Matching — The = Operator, Guards, and Function Clauses

DodaTech Updated 2026-06-29 2 min read

In this tutorial, you will learn about Erlang Pattern Matching. We cover key concepts, practical examples, and best practices to help you master this topic.

Pattern matching is Erlang's most distinctive feature. Variable assignment uses the = match operator, function dispatch is based on pattern matching, and even data access happens through patterns. This approach eliminates entire categories of bugs and makes code declarative.

In this tutorial, you'll learn how pattern matching works throughout Erlang.

What You'll Learn

  • The match operator (=)
  • Single assignment and immutability
  • Pattern matching on tuples, lists, maps
  • Guards (when)
  • Multiple function clauses
  • Case expressions

The Match Operator

%% In Erlang, = is the match operator, NOT assignment
X = 5.     %% Binds X to 5 (first time only)
X = 5.     %% OK: matches (5 = 5)
% X = 6.   %% Error: no match (5 ≠ 6)

%% Matches must succeed
{Name, Age} = {alice, 30}.   %% Name => alice, Age => 30
{Name, 30} = {bob, 30}.     %% Name => bob (matches age)
% {Name, 30} = {bob, 25}.   %% MatchError!

Single Assignment

%% Variables can only be bound once (in the same scope)
X = 10.
% X = 20.  %% Error: X is already bound

%% But rebinding is allowed in different clauses
case Something of
    1 -> X = 10;
    2 -> X = 20
end.

Pattern Matching with Different Types

%% Tuples
{ok, Content} = file:read_file("exists.txt").
{error, enoent} = file:read_file("missing.txt").

%% Lists
[First, Second | Rest] = [1, 2, 3, 4, 5].
First.   %% => 1
Second.  %% => 2
Rest.    %% => [3, 4, 5]

%% Maps
#{name := N, age := A} = #{name => alice, age => 30}.

Guards

%% Guards extend patterns with conditions
factorial(0) -> 1;
factorial(N) when N > 0 -> N * factorial(N - 1).

%% Multiple guard conditions
describe_temp(T) when T < 0 -> freezing;
describe_temp(T) when T >= 0, T < 15 -> cold;
describe_temp(T) when T >= 15, T < 25 -> warm;
describe_temp(T) when T >= 25, T < 35 -> hot;
describe_temp(_) -> very_hot.

%% Guard functions: is_atom/1, is_integer/1, is_list/1, etc.
process(X) when is_integer(X) -> integer;
process(X) when is_float(X) -> float;
process(X) when is_list(X) -> list.

Function Clauses

%% Multiple clauses are tried in order
-module(math).
-export([fib/1]).

fib(0) -> 0;
fib(1) -> 1;
fib(N) when N > 0 -> fib(N-1) + fib(N-2).

%% Pattern matching on arguments
area({circle, R}) -> math:pi() * R * R;
area({rectangle, W, H}) -> W * H;
area({triangle, B, H}) -> B * H / 2.

case Expressions

%% case is pattern matching in expression form
case file:read_file("test.txt") of
    {ok, Content} ->
        io:format("Read ~p bytes~n", [byte_size(Content)]);
    {error, enoent} ->
        io:format("File not found~n");
    {error, Reason} ->
        io:format("Error: ~p~n", [Reason])
end.

Practice Questions

  1. Write a guard that checks if a number is even AND positive.

  2. Write a function with three clauses that processes empty list, single element, and multiple elements.

  3. Use case to handle {ok, _} and {error, _} patterns.

  4. Write a function that takes a tuple {atom, value} and returns different strings based on the atom.

  5. Use pattern matching to extract latitude and longitude from a map.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro