No truthiness: a condition must be true
A language with truthiness turns any value into a condition: zero is false, an empty list is false, an empty string is false. Erlang does not do this. A condition is a boolean expression, and the branch runs only when the value is exactly the atom true. Every other value, including 0 and [], fails the condition instead of counting as false.
There is no truthiness
true and false are ordinary atoms, and a guard accepts only true. The tests that make a condition are comparisons and the type checks; a bare value is not one. The shell shows the two cases side by side:
erl
1> if false -> yes; true -> no end. no 2> if 0 -> yes; true -> no end. no 3> if [] -> yes; true -> no end. no 4> if true -> yes; true -> no end. yes
Lines 2 and 3 do not say that 0 and [] are false: no clause matches, the guard fails because the value is not a boolean, and the true clause answers instead. andalso is stricter still and raises an error on a non-boolean left side, which is why a condition is always built from a comparison.
Zero and the empty list
The number zero is a true value in the sense that it is not false: 0 =:= false is false, and so is [] =:= false. The empty list is the empty string, "" =:= [] is true, and it is not false either. To test for an empty list, compare it with a list or ask length/1:
erl
1> [] =:= "". true 2> [] =:= false. false 3> 0 =:= false. false 4> is_list([]). true 5> length([]). 0
The classic mistake is a condition such as if List -> ... carried over from another language. It fails for every list, empty or not, because a list is not a boolean. Write if List =/= [] -> ... or match the empty list in a clause.
andalso and orelse
andalso and orelse are the short-circuit forms: the right side runs only when the left side cannot decide the answer. That makes them the safe way to build a condition step by step, because a failed first test stops the expression before the dangerous part:
erl
1> false andalso hd([]). false 2> true orelse hd([]). true 3> 1 > 2 andalso 2 / 0 > 1. false
The third line would divide by zero if the right side ran; it does not, because the left side is already false. A plain and has no such promise: it is a strict boolean operator and evaluates both sides. The program below uses andalso to look at the head of a list that may be empty:
checked.erl
-module(checked). -export([main/0, first_or_false/1]). first_or_false(List) -> List =/= [] andalso hd(List) > 0. main() -> io:format("~p~n", [first_or_false([])]), io:format("~p~n", [first_or_false([3])]).
erlc checked.erl
erl -noshell -eval 'checked:main(), halt().'
false true
first_or_false([]) answers false without touching the head, and first_or_false([3]) answers true.
Type checks build the condition
The is_ functions answer a type question with a boolean: is_integer/1, is_binary/1, is_list/1, is_atom/1 and the rest of the family. They are the honest way to test a value, and a function head can put them in a guard. This module classifies any term with one guard per kind:
classify.erl
-module(classify). -export([main/0, kind/1]). kind(Value) when is_integer(Value) -> integer; kind(Value) when is_binary(Value) -> binary; kind(Value) when is_list(Value) -> list; kind(_) -> other. main() -> io:format("~p~n", [kind(42)]), io:format("~p~n", [kind(<<"x">>)]), io:format("~p~n", [kind("x")]), io:format("~p~n", [kind(none)]).
erlc classify.erl
erl -noshell -eval 'classify:main(), halt().'
integer binary list other
A guard that fails moves to the next clause, so the order of the clauses is the order of the tests. "x" is a list before it is a string, which is why the list clause catches it; a binary and a list are different types, as the strings lesson shows. The Guards lesson lists the expressions a guard may hold and why a guard can never have a side effect; the conditions lesson puts them in if and case.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which values make a guard branch run?
Answer
Only the atom
trueA guard needs a boolean, and only
truesucceeds; every other value fails the guard. -
Is the number
0false in Erlang?Answer
No:
0is a number, and0 =:= falseisfalseThere is no truthiness: zero is an ordinary number and only the atom
falseis false. -
What does
[] =:= "". return?Answer
trueA string is a list of codes, and the empty string is the empty list, so the two values are identical.
-
if List -> ok; true -> none endruns withListbound to[1]. What is the answer?Answer
none[1]is not a boolean, so the guard fails and thetrueclause runs. -
What does
false andalso hd([])return?Answer
falseandalsostops at a false left side, so the right side never runs. -
What does
1 > 2 andalso 2 / 0 > 1return?Answer
falseThe left side is false, and
andalsoskips the division that would fail. -
What does
is_list([]).return?Answer
trueThe empty list is still a list, so the type test holds.
-
What does this module print?
-module(q1). -export([main/0, first/1]). first(List) when List =/= [] -> {ok, hd(List)}; first([]) -> empty. main() -> io:format("~p~n", [first([7, 8])]), io:format("~p~n", [first([])]).Answer
{ok,7}thenemptyThe guard keeps
hd/1away from the empty list, so the second call reaches theemptyclause. -
What does this module print?
-module(q2). -export([main/0, kind/1]). kind(Value) when is_integer(Value) -> integer; kind(_) -> other. main() -> io:format("~p~n", [kind(<<"x">>)]).Answer
otherA binary is not an integer, so the guard fails and the catch-all clause answers
other. -
What does this module print?
-module(q3). -export([main/0, first_or_false/1]). first_or_false(List) -> List =/= [] andalso hd(List) > 0. main() -> io:format("~p~n", [first_or_false([])]).Answer
falseList =/= []is false, soandalsoreturns false without callinghd/1.
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.
Автор статьи: Arthur Isaev