Pattern matching

Contents
The match operator
Nested patterns
Wildcards
In function heads

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.

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.

{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.

{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.

area({square, S}) -> S * S;

area({rect, W, H}) -> W * H.

1> area({square, 3}).

9

Next: Guards

Article author: Arthur Isaev

Related articles
Erlang
Erlang course: zero to hero in 50 lessons
What is Erlang and where it runs
Install Erlang and check the version
erl: the interactive shell
Your first Erlang script
Variables and single assignment
Numbers
Atoms
Strings and binaries
Truthiness: only false and nil
Conditions: case and if
Recursion replaces loops
Lists
Tuples
Maps
Functions and clauses
Pattern matching
Guards
Records
Exceptions: try and catch
Modules, exports and imports
Funs: anonymous functions
Higher-order functions
Keyword lists
Erlang idioms and clean code
Rebar3 builds
Testing with EUnit
Debugging
Encodings and Unicode
Time and date
JSON and term formats
Processes and message passing
Spawn and links
Monitors and exits
GenServer
Supervisors
OTP applications
Files and IO
Sockets
HTTP clients
io:format and output
Let it crash: error handling
ETS tables
Timers and timeouts
Code loading
Distributed Erlang
Capstone: todo CLI
Capstone: TCP server
Performance
Modern Erlang tour
Hero roadmap: the whole course on one page
Install Erlang on Windows 11
Install Erlang on Ubuntu
Install Erlang on Rocky Linux
Install Erlang on macOS
Install Erlang on FreeBSD
Erlang in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: