Pattern Matching - Elixir
Pattern Matching - Elixir
HOME
INSTALL
GETTING STARTED
LEARNING
DOCS
BLOG
PACKAGES
Pattern matching
In this chapter, we will show how the = operator in Elixir is actually a match
G ETTI N G ST A RTED
1. Introduction
2. Basic types
operator and how to use it to pattern match inside data structures. Finally, we
3. Basic operators
will learn about the pin operator ^ used to access previously bound values.
4. Pattern matching
5. case, cond and if
lists
8. Modules
iex> x = 1
1
iex> x
1
9. Recursion
10. Enumerables and streams
11. Processes
12. IO and the file system
iex> 1 = x
15. Structs
16. Protocols
iex> 2 = x
** (MatchError) no match of right hand side value: 1
17. Comprehensions
18. Sigils
Notice that 1 = x is a valid expression, and it matched because both the left
and right side are equal to 1. When the sides do not match, a MatchError is
raised.
A variable can only be assigned on the left side of =:
M IX A N D OTP
iex> 1 = unknown
** (RuntimeError) undefined function: unknown/0
1. Introduction to Mix
2. Agent
http://elixir-lang.org/getting-started/pattern-matching.html
1/5
3/8/2016
3. GenServer
4. Supervisor and Application
5. ETS
6. Dependencies and umbrella
Pattern matching
The match operator is not only used to match against simple values, but it is
apps
7. Task and gen-tcp
also useful for destructuring more complex data types. For example, we can
M ETA -PROG RA M MI N G I N
EL IX I R
iex> b
"world"
2. Macros
3. Domain Specific Languages
A pattern match will error in the case the sides cant match. This is, for example,
the case when the tuples have different sizes:
S PON SORS
EL IX I R RA D AR
More interestingly, we can match on specific values. The example below asserts
that the left side will only match the right side when the right side is a tuple that
starts with the atom :ok:
Plataformatec. Subscribe
below.
Elixir
Radar
weekly
Subscribe
now
newsletter
http://elixir-lang.org/getting-started/pattern-matching.html
2/5
3/8/2016
Similar to the hd/1 and tl/1 functions, we cant match an empty list with a
head and tail pattern:
iex> [h|t] = []
** (MatchError) no match of right hand side value: []
The [head | tail] format is not only used on pattern matching but also for
prepending items to a list:
iex> x = 1
1
iex> x = 2
2
http://elixir-lang.org/getting-started/pattern-matching.html
3/5
3/8/2016
The pin operator ^ should be used when you want to pattern match against an
existing variables value rather than rebinding the variable:
iex> x = 1
1
iex> ^x = 2
** (MatchError) no match of right hand side value: 2
iex> {y, ^x} = {2, 1}
{2, 1}
iex> y
2
iex> {y, ^x} = {2, 2}
** (MatchError) no match of right hand side value: {2, 2}
Because we have assigned the value of 1 to the variable x, this last example could
also have been written as:
The variable _ is special in that it can never be read from. Trying to read from
it gives an unbound variable error:
iex> _
http://elixir-lang.org/getting-started/pattern-matching.html
4/5
3/8/2016
iex> length([1,[2],3]) = 3
** (CompileError) iex:1: illegal pattern
This finishes our introduction to pattern matching. As we will see in the next
chapter, pattern matching is very common in many language constructs.
Previous
Top
Next
http://elixir-lang.org/getting-started/pattern-matching.html
5/5