Erlang basics · Lesson 04 (open contents)
Bind a variable once
Learn capitalized variables, single assignment, and pattern matching.
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}.Scorebinds to92.Namebinds to<<"Robin">>.- The final result is
{92, <<"Robin">>}.
Read from the first line down
{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.
Symbols are not secret signs
NameIt begins with a capital letter, so it is a variable.
nameIt 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.
Match each name to its meaning
variable
A name beginning with a capital letter or underscore, such as Name or _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.
Why these forms are useful
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.
Why this shape?
Why is = more than assignment here?
A pattern checks a shape and extracts its data in one move. The same idea works at function boundaries, in case, and in receive.
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.
Close the answer and try
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}.Target result: The result should be {<<"York">>, 28}.
Stuck? Read one hint
Write the atom weather, then the variables City and Temperature.
After you run it, see one 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}.Think about it: 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.
Remember these three lines
- 1
A capitalized name is a variable. A lowercase name is usually an atom.
- 2
An Erlang variable cannot change after it is bound.
- 3
=performs pattern matching.