SoFunction
Updated on 2025-04-08

Summary of matching patterns in Erlang

1. Matching during assignment

Atomic Matching

Copy the codeThe code is as follows:

atom    = atom                        % atom
another = another                     % another
atom    = another                     % exception error

Variable matching
Copy the codeThe code is as follows:

Var = 2.                              % 2
Var = 3 - 1.                          % 2
Var = 1.                              % exception error

Tuple Match
Copy the codeThe code is as follows:

Attr = {name, sloger}.                % {name, sloger}
{name, Name} = Attr.                  % {name, sloger}
Name.                                 % sloger

List Match
Copy the codeThe code is as follows:

Langs = [perl, python, ruby, erlang].
[Head | Tail] = Langs.
Head.                                 % perl
Tail.                                 % [python, ruby, erlang]

Parameter matching
Copy the codeThe code is as follows:

sum([]) -> 0;
sum([H|T]) -> H + sum(T).

sum([1, 2, 3]).                       % 6


Record matching
Copy the codeThe code is as follows:

%% record(post, {title, slug, body, author}).

Post = #post{title = "Pattern Match in Erlang",
             slug = "pattern-match-in-erlang",
             body = "Bla bla bla...",
             author = sloger}.

#post{title = Title, slug = Slug} = Post.

Title.                                                                                                                                                                                                                                                             �
Slug.                                 % "summary-of-pattern-match-in-erlang"


Bit Match
Copy the codeThe code is as follows:

Red = 5.
Green = 23.
Blue = 200.

Color = <<Red:5, Green:6, Blue:5>>.

<<R1:5, G1:6, B1:5>> = Color.

R1.                                   % 5
G1.                                   % 23
B1.                                   % 200


2. Matching in process control

if

Copy the codeThe code is as follows:

if
    Pattern1 [when Guard1] -> Expression1;
    Pattern2 [when Guard2] -> Expression2;
    %% and so on ...
_                                                                                                                              �
end.


case

Copy the codeThe code is as follows:

case Expression of
    Pattern1 [when Guard1] -> Expression1;
    Pattern2 [when Guard2] -> Expression2;
    %% and so on ...
    _                      -> Expression3
end.


try catch

Copy the codeThe code is as follows:

try FunctionOrExpressions of
    Pattern1 [when Guard1] -> Expression1;
    Pattern2 [when Guard2] -> Expression2
    %% and so on ...
catch
    ExType:ExPattern1 [when ExGuard1] ->
        ExExpression1;
    ExType:ExPattern2 [when ExGuard2] ->
        ExExpression2;
    %% and so on ...
_:_ -> DefaultExExpression
after
    AfterExpressions
end

Message Matching

Copy the codeThe code is as follows:

loop() ->
    receive
        {From, {rectangle, Width, Height}} ->
            From ! {self(), Width * Height},
            loop();
        {From, {circle, R}} ->
            From ! {self(), 3.14 * R * R},
            loop();
        {From, _Other} ->
            From ! {self(), {error, unknown_shape}}
            loop()
    end.
Pid = spawn(fun loop/0).
Pid ! {self(), {rectangle, 10, 5}}.         % {Pid, 50}
Pid ! {self(), {circle, 4}}.                % {Pid, 50.24}
Pid ! {self(), {square, 10}}.               % {Pid, {error, unknown_shape}}