Truthiness: only false and nil

Only two false values

In Erlang exactly two values count as false: the atom false and the empty list []. Everything else is true, including zero, nil, and the empty binary.

This is stricter than most languages, so conditions never trip over a zero by accident.

erl

1> 0.
0
2> is_list([]).
true

Short-circuit and

andalso and orelse are the short-circuit forms. The right side runs only when the left side cannot decide the answer.

Use them to guard access, just like the and and or of other languages.

erl

1> X = [], X =/= [] andalso X > 0.
false
2> true orelse error(boom).
true

In guards

Guard expressions in case and if see the same truthiness. A guard sequence succeeds when its last test holds.

Lesson 17 covers guards in full, including which functions may appear inside one.

erl

1> case [] of
2> [] -> empty;
3> _ -> nonempty
4> end.
empty

Type checks

The is functions answer type questions: is_atom, is_integer, is_binary, and the rest of the family. They are the honest way to test a value.

Pair them with the comparison operators of lesson 7 and every branch of your code states exactly what it expects.

erl

1> is_integer(42).
true
2> is_binary(<<"text">>).
true

Next: Conditions: case and if

Questions

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

  1. Which two values are false in Erlang?

    Answer

    false and []

    Only the atom false and the empty list [] count as false; every other term is true, including nil.

  2. Is the number 0 true or false in a condition?

    Answer

    True, because only two values are false

    Zero is an ordinary term, and only false and nil are false.

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

    Answer

    true

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

  4. What does true orelse error(boom). return?

    Answer

    true

    orelse stops as soon as the left side is true, so the failing right side never runs.

  5. What does false andalso error(boom). do?

    Answer

    Returns false without calling error/1

    andalso skips the right side when the left side is false, which is the point of the short-circuit form.

  6. What does X = [], X =/= [] andalso X > 0. return?

    Answer

    false

    X becomes [], so X =/= [] is false and the rest of the expression is skipped.

  7. Which function tests for a whole number?

    Answer

    is_integer/1

    is_integer/1 answers true for integers only; is_number/1 also accepts floats.

  8. What does is_binary(<<"text">>). return?

    Answer

    true

    The value is a binary, so the type test holds.

  9. Why is andalso safer than and for a guarded access?

    Answer

    It skips the right side when the left side decides

    The short-circuit form never evaluates the right side when the left side is already false.

  10. What does the case [] of example return?

    Answer

    empty

    The value [] matches the first pattern, so the body empty is returned.

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