A pattern checks a shape and extracts its data in one move. The same idea works at function boundaries, in case, and in receive.
Bind a variable once: full examples and further reading
The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.
Learn capitalized variables, single assignment, and pattern matching.
Run tuple and map matches in erl, then confirm that a bound variable only matches the same value.
The left side is a shape; the right side is data
A map uses => when it is created and := to match an existing key.
%% Shell only: clear variables left by earlier lessons
f().
%% Match a tuple and one existing map key
{ok, Score} = {ok, 92}.
#{name := Name} = #{name => <<"Robin">>, age => 12}.
%% A bound variable can only match the same value
92 = Score.
{Score, Name}.Score binds to 92.
Name binds to <<"Robin">>.
The final result is {92, <<"Robin">>}.
Steps for the full example
{ok, Score}requires a two-item tuple whose first item is the atomok.#{name := Name}is a map pattern. It finds the existing keynameand letsNamereceive its value.92 = Scoresucceeds becauseScoreis already 92.93 = Scorewould causebadmatch.
Original lesson notes
An Erlang variable must begin with a capital letter or an underscore. Score is a variable. score is an atom.
Once an Erlang variable is bound in a scope, it cannot be rebound to a different value. This is called single assignment.
= is the match operator. The left side gives a shape and the right side gives data. If the shapes do not fit, Erlang reports badmatch.
Symbols and terms
Name- It begins with a capital letter, so it is a variable.
name- It begins with a lowercase letter, so it is an atom.
_- Match this position without saving its value.
f().- Use this only in the Erlang shell to clear the shell's current variable bindings.
- variable
- A name beginning with a capital letter or underscore, such as
Nameor_Rest. - single assignment
- Once a variable is bound, it cannot be bound to a different value in the same scope.
- matching
- Check two shapes and let new variables receive values from matching positions.
Original full exercise
Unpack the city and temperature from {weather, <<"York">>, 28}.
%% Clear variables left by the example and earlier lessons
f().
%% The fixed label must match; variables begin with capitals
{____, ____, ____} = {weather, <<"York">>, 28}.
%% Look at the unpacked values
{City, Temperature}.The result should be {<<"York">>, 28}.
Show hint
Write the atom weather, then the variables City and Temperature.
Show original answer
%% f() belongs in the shell, not in an .erl file
f().
%% The atom checks the label; variables receive the data
{weather, City, Temperature} = {weather, <<"York">>, 28}.
%% The two variables are now bound
{City, Temperature}.After Count = 3., what happens if you run Count = 4.?
Erlang reports badmatch. Count is already bound to 3 and cannot receive the different value 4.
Original lesson takeaways
- A capitalized name is a variable. A lowercase name is usually an atom.
- An Erlang variable cannot change after it is bound.
=performs pattern matching.
Design trade-offs and language comparisons
Why this shape?
Why is = more than assignment here?
The rules differ across a new match: Elixir rebinds a plain name and needs ^name to check the old value; an Erlang variable becomes a check after its first binding.
Coming from Java, Python, or JavaScript
Java
- Familiar starting point
- Assign a local variable, then inspect an object with accessors or instanceof.
- What BEAM changes
- A pattern checks and unpacks tuples, lists, or maps directly.
- False friend
- This is not method overloading, and it does not mutate the value.
Python
- Familiar starting point
- Unpacking and match/case offer a familiar starting point.
- What BEAM changes
- Data is not changed in place, and patterns are routine tools for function clauses and message protocols.
- False friend
- Elixir can rebind a name without mutating its old value; a bound Erlang variable checks the next match.
JavaScript
- Familiar starting point
- Array and object destructuring mainly extract values.
- What BEAM changes
- BEAM patterns can also require fixed tags; a mismatch selects another clause or fails.
- False friend
- JavaScript destructuring defaults follow different rules.