Variables and single assignment
A variable in Erlang starts with an upper-case letter or an underscore. The equals sign does not store into it: it matches the right side against the left and binds a name that is still free. Once a name has a value, the value never changes, and that is the rule the whole language is built on.
Bind once
The first match binds the name, and the expression evaluates to the right-hand value, so the shell prints that value:
erl
1> Name = "Ada". "Ada" 2> Answer = 42. 42 3> Name. "Ada" 4> Answer + 1. 43
A name that starts with a lower-case letter is not a variable but an atom, a value of its own. name = "Ada" fails, because name is the atom name and an atom does not match a string. The Atoms lesson takes the two apart.
No rebinding
A second match with the same value succeeds and prints the value again. A second match with another value fails, because the name already holds the first one. The shell reports the failure as an exception and keeps running:
erl
1> Answer = 42. 42 2> Answer = 42. 42 3> Answer = 43. ** exception error: no match of right hand side value 43 4> Answer. 42
The message names the value that did not fit; the old binding is untouched. This looks strict at first and pays off in concurrent code: a term that a process holds cannot be changed by another process, so two processes can never race over the same slot.
A repeated name in a pattern
The same rule works inside a pattern. A name that appears twice in one pattern must match the same value both times, which is a compact way to compare two arguments:
same.erl
-module(same). -export([main/0, same/2]). same(X, X) -> true; same(_, _) -> false. main() -> io:format("~p~n", [same(1, 1)]), io:format("~p~n", [same(1, 2)]).
erlc same.erl
erl -noshell -eval 'same:main(), halt().'
true false
In same(X, X) the first argument binds X and the second must be equal to it, so the clause matches only when both are the same term. same(1, 2) falls through to the second clause. The Pattern matching lesson builds on this form.
The underscore
The lone underscore matches anything and binds nothing. It says: I do not care about this part. An underscore with a name, like _Reason, binds the value too, and the leading underscore tells the compiler that ignoring it is intended, so an unused name does not warn.
erl
1> {ok, _} = {ok, 7}. {ok,7} 2> {error, _Reason} = {error, enoent}. {error,enoent}
The two forms differ in what they keep: _ holds no value at all, while _Reason does. A pattern may contain _ several times, but not the same named variable twice unless the values are equal.
Where variables live
A binding lives in the function clause that made it. Another clause of the same function starts with a clean slate, even when it uses the same name. Both clauses of scope.erl bind a variable called Word, and neither clause can see the other binding:
scope.erl
-module(scope). -export([label/1]). label(0) -> Word = "zero", Word; label(_) -> Word = "other", Word.
erlc scope.erl
erl -noshell -eval 'io:format("~p~n", [scope:label(0)]), io:format("~p~n", [scope:label(5)]), halt().'
"zero" "other"
A value bound in a function head is visible in the whole clause, and a value bound in the body is visible from that line to the end of the clause. The shell is one long interaction and keeps a binding until f/0 or f/1 forgets it, which the shell lesson shows.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which names are variables in Erlang?
Answer
Names starting with an upper-case letter or an underscore
A lower-case name is an atom; variables begin with an upper-case letter or
_, as inNameand_Reason. -
What does
=do inAnswer = 42.?Answer
Matches the right side against the left and binds the unbound name
The match operator tries to make both sides equal, binding
Answerbecause it is still unbound. -
What does the shell answer to
Name = "Ada".?Answer
"Ada"The match succeeds and evaluates to the right-hand value, which the shell prints as
"Ada". -
Answer = 42.is followed byAnswer = 43.. What does the shell print?Answer
** exception error: no match of right hand side value 43The name is bound to 42, so matching 43 against it fails with a badmatch exception.
-
What does the lone underscore
_bind?Answer
Nothing: it matches any value
_is the anonymous variable: it matches anything and keeps no value, so it can appear many times in one pattern. -
What is special about a name like
_Reason?Answer
It binds the value and suppresses the unused-variable warning
The leading underscore keeps the value readable in the code while telling the compiler that not using it is intended.
-
What does this module print?
-module(q1). -export([main/0, same/2]). same(X, X) -> true; same(_, _) -> false. main() -> io:format("~p~n", [same(1, 1)]), io:format("~p~n", [same(1, 2)]).Answer
truethenfalseThe repeated name matches only equal arguments, so
same(1, 1)is true andsame(1, 2)reaches the second clause. -
What does this module print?
-module(q2). -export([main/0]). swap({A, B}) -> {B, A}. main() -> {Left, Right} = swap({1, 2}), io:format("left ~p, right ~p~n", [Left, Right]).Answer
left 2, right 1The function returns
{2,1}, and the pattern bindsLeftto2andRightto1. -
What does this module print?
-module(q3). -export([main/0]). main() -> X = 1, Y = X + 1, io:format("~p~n", [Y]).Answer
2Xis bound to1,Yto1 + 1, and the module prints2. -
How long does a binding in a function clause live?
Answer
For the rest of that clause; another clause starts fresh
Each clause has its own scope, so the same name in another clause is a new, unbound variable.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev