Erlang basics · Lesson 04 (open contents)
01 · Open erl02 · Meet common terms03 · Tell two kinds of text apart04 · Bind a variable once05 · Split and filter a list06 · Parse a number safely07 · Choose one path with case08 · Let a function keep going09 · Put code in a module10 · Compare terms and Boolean results11 · Read and update nested maps12 · Build and inspect UTF-8 binaries13 · Let clauses choose the function path14 · Solve a list twice15 · Choose with patterns, guards, and errors16 · Describe a module and one record17 · Put one tested module in Rebar318 · Return success or failure as data19 · Read and write one file safely20 · Build a data pipeline with functions21 · Keep records behind a module boundary22 · Define a module contract23 · Describe the data contract24 · Test small rules and whole flows25 · Shape builds and make a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ERLANG · SCRATCH · LESSON 0425 minutes

Bind a variable once

Learn capitalized variables, single assignment, and pattern matching.

01 · Start with the whole example

The left side is a shape; the right side is data

A map uses => when it is created and := to match an existing key.

Erlang
%% 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}.
Check the result first
  • Score binds to 92.
  • Name binds to <<"Robin">>.
  • The final result is {92, <<"Robin">>}.
02 · Take the code apart

Read from the first line down

  1. {ok, Score} requires a two-item tuple whose first item is the atom ok.
  2. #{name := Name} is a map pattern. It finds the existing key name and lets Name receive its value.
  3. 92 = Score succeeds because Score is already 92. 93 = Score would cause badmatch.
03 · Meet the new symbols

Symbols are not secret signs

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.

04 · Ideas inside the code

Match each name to its meaning

01

variable

A name beginning with a capital letter or underscore, such as Name or _Rest.

02

single assignment

Once a variable is bound, it cannot be bound to a different value in the same scope.

03

matching

Check two shapes and let new variables receive values from matching positions.

05 · Make the idea clear

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.

DESIGN DECISION

Why this shape?

Why is = more than assignment here?

Choice

A pattern checks a shape and extracts its data in one move. The same idea works at function boundaries, in case, and in receive.

Cost and boundary

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.

A FAMILIAR POINT OF VIEW

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.
06 · Change it yourself

Close the answer and try

Unpack the city and temperature from {weather, <<"York">>, 28}.

Practice starting point
%% 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
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.

Take with you

Remember these three lines

  1. 1

    A capitalized name is a variable. A lowercase name is usually an atom.

  2. 2

    An Erlang variable cannot change after it is bound.

  3. 3

    = performs pattern matching.

Lesson completeRun the code once, then mark this lesson as done.