← Back to lesson 06: Make the shapes match

Make the shapes match: full examples and further reading

The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.

Treat `=` as a match and unpack tuples, lists, and maps.

Run three pattern matches that unpack a tuple, a list, and a map into new names.

Unpack three kinds of collections

Read the shape on the left first. Then find each matching value on the right.

Original full example
# Save the tag we expect
expected_tag = :ok

# ^ checks the old expected_tag value; _ ignores the third value
{^expected_tag, score, _} = {:ok, 92, :midterm}

# Split the first item from the rest of a list
[first | rest] = ["peach", "plum", "apricot"]

# A map pattern names only the key we need
%{name: name} = %{name: "Robin", age: 12}

{score, first, rest, name}

score becomes 92.

first becomes "peach", and rest becomes ["plum", "apricot"].

name becomes "Robin".

Steps for the full example

  1. ^expected_tag does not bind the name again. It requires the tuple's first item to equal the existing :ok; _ ignores the third value.
  2. [first | rest] matches only a non-empty list. The bar separates the first item from the remaining list.
  3. A map pattern can name only the keys it needs. The extra age key does not stop this match.

Original lesson notes

In Elixir, = does more than assign a value. It checks whether the shapes on both sides can match.

The left side is like a form with empty spaces. The data on the right fills them. If a position, label, or key does not fit, the match fails.

This is often used to unpack data. Write only the parts you need. An underscore _ can take the rest.

A plain name in a pattern receives a value. To check against a value that name already holds, add the pin operator ^. ^expected_tag means “this position must equal the old value of expected_tag.”

Symbols and terms
=
Match the two sides. New names on the left bind to the matching values.
_
The value in this position does not need to be saved.
[head | tail]
Split a non-empty list into its first item and the remaining items.
^name
Check this position against the value already stored in name. Read it as pin name.
pattern
A shape on the left that checks and unpacks the data on the right.
destructuring
Taking several values out of a collection at once.
match failure
When the shapes or fixed values do not line up, Elixir raises a MatchError.
pin operator
^name uses the value already bound to name instead of binding a new value in that position.

Original full exercise

Bind the expected tag, then check it while unpacking the city and temperature from {:weather, "York", 28}.

Exercise starter
# Accept only a weather message
expected_tag = :weather

# Pin the old tag and name the other two positions
{^____, ____, ____} = {:weather, "York", 28}

# Return the city and temperature
{city, temperature}

The result should be {"York", 28}.

Show hint

Put ^expected_tag first, followed by city and temperature.

Show original answer
Reference answer
# Save the tag we plan to check
expected_tag = :weather

# ^expected_tag must match; two new names receive the data
{^expected_tag, city, temperature} = {:weather, "York", 28}

# Look at the values we unpacked
{city, temperature}
Will {:ok, value} = {:error, 3} succeed?

No. The left side requires :ok, but the right side begins with :error, so Elixir raises a MatchError.

Original lesson takeaways
Design trade-offs and language comparisons
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.
Original course references