Operators and the guard-safe subset

The operators of Elixir are the ones of a calculator plus the ones of a pattern language. Arithmetic and comparison are familiar, the two equal signs are not, and in is the membership test that shows up in guards. This lesson lists the operators a script uses, shows the precedence that decides a mixed line, and ends with the subset a guard accepts.

Arithmetic and precedence

+, -, * and / work as they do in arithmetic, with multiplication before addition and parentheses to change the order. ** is the exponent and groups to the left, so a chain without parentheses is not the tower a mathematician would write:

arithmetic.exs

IO.puts(1 + 2 * 3) IO.puts((1 + 2) * 3) IO.puts(2 ** 3 ** 2) IO.puts(10 / 4) IO.puts(rem(10, 4)) IO.puts(-5 + 2)

elixir arithmetic.exs

7
9
64
2.5
2
-3

2 ** 3 ** 2 is (2 ** 3) ** 2 and gives 64, not 512, so write the parentheses when the tower is what you mean. The slash returns a float and rem/2 keeps the remainder; the numbers lesson covered the division in full.

== and === in comparison

Two equal signs compare values and two more compare values and types. == treats 1 and 1.0 as equal because they are the same number, and === refuses because one is an integer and the other a float. The negations are != and !==:

comparison.exs

IO.puts(1 == 1.0) IO.puts(1 === 1.0) IO.puts("abc" < "abd") IO.puts(:a < "b") IO.puts([1, 2] < [1, 3])

elixir comparison.exs

true
false
true
true
true

The last three lines compare values of different shapes, and the language has an order for every pair of types. Numbers come first, then atoms, then tuples, maps, lists and binaries; two binaries compare byte by byte, and two lists element by element. That order is what makes sorting any list possible, and it is also the source of the surprise in the last section.

Boolean operators and in

and, or and not are the strict boolean operators; &&, || and ! accept any value and follow the truthiness rule. in asks whether a value is an element of a list or a key of a map:

boolean.exs

IO.puts(true and false) IO.puts(true or false) IO.puts(not false) IO.puts(1 in [1, 2, 3]) IO.puts(:b in %{a: 1, b: 2}) IO.puts(nil || "default")

elixir boolean.exs

false
true
true
true
false
default

The map line is false because in tests the keys, and the key :b is not there. A range works as a collection too, so 3 in 1..5 is true. The last line is the default idiom of the truthiness lesson, kept here because the operator is the same one.

The subset a guard may use

A guard is the when part of a function clause, and it accepts only expressions the runtime can evaluate without running arbitrary code. The safe set is small:

A function outside the set, such as String.length/1 or a function of your own, cannot appear in a guard. The guards lesson shows the compiler error a reader gets when one does, and how to move the test into the body when the guard is not enough.

The mistake: comparing two types

A comparison between a binary and a number does not raise and does not convert. It answers with the term order, where every number comes before every binary, so the text "10" is not less than the number 9:

mixed.exs

IO.puts("10" < 9)

elixir mixed.exs

false

A reader expects true, because ten is more than nine; the program says false, because a binary is never less than a number. Convert before the comparison, with String.to_integer/1, and the line does what the reader meant. The same silence hides in a sort of mixed data, which is why a list that holds both numbers and text is usually a bug.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

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

  1. What is the difference between == and ===?

    Answer

    == compares values, === compares values and types as well

    1 == 1.0 is true, while 1 === 1.0 is false because the types differ.

  2. What does in test for a map?

    Answer

    Whether the value is one of the keys

    For a map, in checks the key set; for a list it checks the elements.

  3. Which operators are strict about booleans?

    Answer

    and, or and not

    The word forms require true or false; the symbol forms accept any value.

  4. Which of these may appear in a guard?

    Answer

    is_integer/1 and a comparison

    A guard may call only the small safe set, which includes the is_* checks and comparisons.

  5. What does this program print? IO.puts(1 + 2 * 3)

    Answer

    7

    Multiplication runs before addition, so the line is 1 + 6.

  6. What does this program print? IO.inspect(1 == 1.0)

    Answer

    true

    The loose comparison treats the integer and the float as the same number.

  7. What does this program print? IO.inspect(1 === 1.0)

    Answer

    false

    The strict comparison also compares the types, and an integer is not a float.

  8. What does this program print? IO.puts(2 ** 3 ** 2)

    Answer

    64

    The exponent groups to the left, so the line is (2 ** 3) ** 2.

  9. What does this program print? IO.puts(1 in [1, 2])

    Answer

    true

    The list holds the value 1, so the membership test is true.

  10. What does this program print? IO.puts("10" < 9)

    Answer

    false

    A number sorts before a binary in the term order, so the binary is not less than the number.

Article author: Arthur Isaev