Variables and single assignment

Bind once

Variables start with an uppercase letter or an underscore. The equals sign binds a name to a value, and the name then stands for that value everywhere in its scope.

erl

1> Name = "Ada".
"Ada"
2> Answer = 42.
42

No rebinding

Single assignment means a bound name never changes. The equals sign is a match: it checks that the right side equals the stored value, and fails loudly when it does not.

This looks strict at first and pays off in concurrent code, where two processes can never race over a mutable cell.

erl

1> Answer = 42.
42
2> Answer = 42.
42
3> Answer = 43.
** exception error: no match of right hand side value 43

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 you mean to ignore it, so an unused name does not warn.

erl

1> {ok, _} = {ok, 7}.
{ok,7}
2> {error, _Reason} = {error, enoent}.
{error,enoent}

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 runs the same name.

The shell is one big clause, which is why a name typed once stays bound until you restart erl.

Save this module as scope.erl. Both clauses bind a variable called Word, and neither clause can see the other binding:

-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"

Next: Numbers

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. 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 in Name and _Reason.

  2. What does = do in Answer = 42.?

    Answer

    Matches the right side against the left and binds the unbound name

    The match operator tries to make both sides equal, binding Answer because it is still unbound.

  3. 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".

  4. Answer = 42. is followed by Answer = 42.. What happens?

    Answer

    The shell returns 42, because the values match

    A match with an already bound name succeeds when both sides are equal, so the second line prints 42 again.

  5. Answer = 42. is followed by Answer = 43.. What does the shell print?

    Answer

    ** exception error: no match of right hand side value 43

    The name is bound to 42, so matching 43 against it fails with a badmatch exception.

  6. 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.

  7. What does {ok, _} = {ok, 7}. return?

    Answer

    {ok,7}

    The tuple matches, _ ignores the 7, and the expression evaluates to the whole tuple {ok,7}.

  8. 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.

  9. 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.

  10. Why does single assignment help concurrent programs?

    Answer

    No process can change a value that another process already holds

    A value never changes after it is bound, so two processes can never race over the same mutable cell.

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