F1 · Algebra
Equations & pattern matching
Some equations hold for every value; others you solve for the few that fit. Pattern matching is the second kind made structural — given a shape, find the bindings that make both sides agree.
An identity like (a+b)² = a² + 2ab + b² is true for all a and b; a conditional equation like x + 3 = 5 is solved by finding x. Elixir’s match operator solves by structure: it lines a pattern up against a value, binds the variables, and routes control to the clause whose shape fits. We pull a structure apart, dispatch on shape, and refine with guards.
Destructuring
A pattern is a template that mirrors the shape of a value, with variables where you want the parts. When the shapes line up, each variable binds to the piece it sits over — this is destructuring. A literal in the pattern is not a variable; it must match the value exactly, and matches without binding anything.
Choose a pattern and see what each variable picks out.
- pattern
- a template mirroring a value’s shape, with variables for the parts.
- destructure
- bind variables to the pieces of a structure by matching it.
- clause
- one of several definitions; the one whose pattern matches is the one that runs.
- guard
- a
whencondition that further restricts when a clause matches.
Destructuring · a pattern picks out the parts
Algebra
Solving by structure: find the values that make the pattern and the data the same.
Elixir
{a, b} = pair, [h | t] = list, %{key: v} = map — the variables bind.
A pattern reads like the value it matches, with holes for the parts you want. Matching fills the holes; literals only have to agree.
Dispatch by shape
A piecewise definition gives a different answer for each case. Elixir writes one with several clauses,
and the value’s shape chooses which clause runs — the first whose pattern matches, top to bottom. This
is how F1.06’s recursion told the empty list from a non-empty one, and it is the everyday way to handle
an {:ok, _} result apart from an {:error, _} one.
Feed a value in and watch which clause it lands on.
case · the first matching clause wins
Algebra
A piecewise definition: the case that applies is the one whose condition the input meets.
Elixir
case and multi-clause functions pick the first matching pattern, top to bottom.
Pattern matching is control flow. The shape of the value decides the branch, with no explicit conditional in sight.
Guards
Sometimes the shape is the same but the answer depends on the value. A guard — a
when clause — adds a condition that a match must also satisfy. The same parameter
n can route three ways depending on its sign.
Slide n and see which guarded clause it satisfies.
when · a condition on the match
Algebra
A case split by condition: same form, different branch depending on the value.
Elixir
def sign(n) when n > 0, do: :positive — a guard refines the match.
A guard lets a match depend on the value, not only the shape. Pattern plus guard is most of the branching functional code ever needs.
What this lands
Pattern matching is solving by structure: line a pattern up with a value, bind the variables that make them agree, and let the matching shape choose the clause. Destructuring pulls a tuple, list, or map apart; dispatch routes control by shape, the same mechanism F1.06’s recursion used to separate base from step; and guards refine a match by the value. With F1.04’s match operator behind it, this is the branching backbone of the language. It completes the algebra of F1 — the chapter closes with a plotting lab where you draw functions and watch composition as curves.
Next: F1.09 — Functions on the plane (planned) — the chapter’s lab: plot and compose functions and watch f∘g as curves. The rest of F1 is on the chapter page.