Pattern Matching & Guards
Master Erlang's pattern matching and guard clauses, the foundation of expressive, branch-free Erlang code.
Pattern Matching & Guards is a free Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Erlang OTP: Distributed & Fault-Tolerant Systems Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Matching, Not Assigning
In Erlang = is the match operator, not assignment: it binds left-side variables to right-side values, or fails if they cannot match.
X = 42.
% X is now bound to 42Destructuring Tuples
Pattern matching can destructure compound terms like tuples in a single step, pulling values straight out.
{ok, Value} = {ok, 100}.
% Value is bound to 100Matching Lists
The [Head | Tail] pattern splits a list into its first element and the remaining rest.
[First | Rest] = [1, 2, 3].
% First = 1, Rest = [2, 3]The Pin / Bound Match
Once a variable is bound, reusing it in a pattern matches against its value rather than rebinding it.
X = 5.
{X, Y} = {5, 10}. % matches, Y = 10
{X, Y} = {7, 10}. % fails: X is bound to 5The Underscore Wildcard
The underscore wildcard _ matches anything and binds nothing — use it for parts you do not care about.
{_, Important} = {ignore_me, 99}.
% Important = 99Function Clause Matching
Function clauses are tried top to bottom, choosing the first whose arguments match. This replaces many if/else chains.
describe(0) -> zero;
describe(1) -> one;
describe(_) -> many.What Are Guards?
Guards add extra conditions to a clause with when; the clause fires only if both the pattern and the guard succeed.
classify(N) when N > 0 -> positive;
classify(N) when N < 0 -> negative;
classify(_) -> zero.Allowed Guard Expressions
Guards must be side-effect-free — only comparisons, arithmetic, and type tests like is_integer or is_list are allowed.
kind(X) when is_integer(X) -> int;
kind(X) when is_list(X) -> list;
kind(_) -> other.Combining Guards
Combine guard tests with , for AND and ; for OR.
in_range(X) when X >= 1, X =< 10 -> yes;
in_range(_) -> no.Matching in case
The case expression matches a value against patterns (with optional guards) and returns the branch that fits.
case lists:keyfind(id, 1, List) of
{id, V} -> V;
false -> not_found
end.Why It Matters
Pattern matching and guards make Erlang declarative — you describe data shapes instead of branching, the same skill you will use for messages.
Quick Check
Test your matching knowledge.
Recap
Recap: = matches and binds, you destructure tuples and lists, and function clauses with guards replace manual branching.
Frequently asked questions
Is the “Pattern Matching & Guards” lesson free?
Yes — the full text of “Pattern Matching & Guards” is free to read here on the web, and the Erlang OTP: Distributed & Fault-Tolerant Systems Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Erlang OTP: Distributed & Fault-Tolerant Systems Programming course, upgrade to CoddyKit PRO.
What will I learn in “Pattern Matching & Guards”?
Master Erlang's pattern matching and guard clauses, the foundation of expressive, branch-free Erlang code. You practise Erlang OTP: Distributed & Fault-Tolerant Systems Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
No prior experience is required. Erlang OTP: Distributed & Fault-Tolerant Systems Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Pattern Matching & Guards” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson?
Yes. Every Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Erlang & VM
- Erlang Processes & Messaging
- Basic Concurrency Patterns
- Pattern Matching & Guards