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.

  1. Which values make a guard branch run?

    Answer

    Only the atom true

    A guard needs a boolean, and only true succeeds; every other value fails the guard.

  2. Is the number 0 false in Erlang?

    Answer

    No: 0 is a number, and 0 =:= false is false

    There is no truthiness: zero is an ordinary number and only the atom false is false.

  3. What does [] =:= "". return?

    Answer

    true

    A string is a list of codes, and the empty string is the empty list, so the two values are identical.

  4. if List -> ok; true -> none end runs with List bound to [1]. What is the answer?

    Answer

    none

    [1] is not a boolean, so the guard fails and the true clause runs.

  5. What does false andalso hd([]) return?

    Answer

    false

    andalso stops at a false left side, so the right side never runs.

  6. What does 1 > 2 andalso 2 / 0 > 1 return?

    Answer

    false

    The left side is false, and andalso skips the division that would fail.

  7. What does is_list([]). return?

    Answer

    true

    The empty list is still a list, so the type test holds.

  8. 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} then empty

    The guard keeps hd/1 away from the empty list, so the second call reaches the empty clause.

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

    other

    A binary is not an integer, so the guard fails and the catch-all clause answers other.

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

    false

    List =/= [] is false, so andalso returns false without calling hd/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.

Article author: Arthur Isaev