Comparisons and equality
Erlang can compare any two terms: the language defines one total order over numbers, atoms, tuples, lists, maps and binaries, and lists:sort/1 uses it without asking what the terms are. Equality has two forms, == and =:=, and the difference between them is the integer one and the float one point zero.
Two kinds of equality
== compares values and converts numbers, so 1 == 1.0 is true. =:= compares value and type, so 1 =:= 1.0 is false. Their negations are /= and =/=. This program prints all four:
equal.erl
-module(equal). -export([main/0, loose/2, same/2]). loose(A, B) -> A == B. same(A, B) -> A =:= B. main() -> io:format("~p~n", [loose(1, 1.0)]), io:format("~p~n", [same(1, 1.0)]), io:format("~p~n", [loose("abc", <<"abc">>)]), io:format("~p~n", [loose(1, 2)]), io:format("~p~n", [same(2, 2)]).
erlc equal.erl
erl -noshell -eval 'equal:main(), halt().'
true false false false true
The third line is not a number comparison at all: a list and a binary are different types, and == does not convert them, so the two spellings of abc are not equal. The rule of thumb for a program is =:=: it never changes the type of anything, so a test means what it says. Use == only where a float and an integer must count as the same value, as in a tolerance check written as abs(A - B) < 0.001.
The order of terms
The comparison operators work on every pair of terms, even unrelated ones. The order is fixed: number, then atom, then reference, fun, port, pid, tuple, map, nil, list and bitstring; inside a type the values compare in their natural order, and tuples compare element by element. That is why 1 < a is true and why a sort never fails on mixed data:
order.erl
-module(order). -export([main/0]). main() -> Terms = [b, 2, "text", {a}, #{}, [], 1.5, <<"x">>], io:format("~p~n", [lists:sort(Terms)]).
erlc order.erl
erl -noshell -eval 'order:main(), halt().'
[1.5,2,b,{a},#{},[],"text",<<"x">>]
The numbers come first, the atom b after them, then the tuple, the map and the empty list, and the string "text" before the binary. The empty list is [], the nil of the order, and sorts before any non-empty list. A map compares by size first and then key by key, which is enough to sort a list of maps but rarely the order a reader expects.
Strings compare by codes
A string is a list of character codes, so < and > compare the lists code by code, as a dictionary compares letters. The comparison stops at the first difference, and a shorter prefix sorts first:
erl
1> "abc" < "abd". true 2> "apple" < "banana". true 3> "ab" < "abc". true 4> [] < [1]. true 5> {1, 2} < {1, 3}. true
This is the order lists:sort/1 and lists:max/1 use, so a list of words sorts alphabetically without a comparator function. It is byte order for a binary and code order for a list, which matters for accented letters: both compare UTF-8 codes, not a locale collation. The encoding lesson takes that further.
Comparisons in a guard
A comparison is a boolean expression, so it is exactly what a guard needs. This module picks the larger of two terms with two clauses and one guard, and it works for numbers and for strings because the order is total:
compare.erl
-module(compare). -export([main/0, max_of/2]). max_of(A, B) when A >= B -> A; max_of(_, B) -> B. main() -> io:format("~p~n", [max_of(3, 7)]), io:format("~p~n", [max_of("apple", "banana")]).
erlc compare.erl
erl -noshell -eval 'compare:main(), halt().'
7 "banana"
The library has the same function as erlang:max/2, and lists:max/1 folds it over a list. Writing it by hand once is worth it: the guard is the whole condition, and the second clause catches every pair the first one does not match. The Guards lesson lists which expressions fit in a guard.
The <= mistake
Most languages write less-than-or-equal as <=. Erlang writes it as =<, following the arrow of its comparison operators. The other form is a syntax error, not a wrong answer, and the shell points at the character:
erl
1> 1 =< 1. true 2> 1 <= 1. * 1:3: syntax error before: '<='
The same holds in a guard: when A =< B -> is correct, when A <= B -> does not compile. The greater-than-or-equal form is the ordinary >=, so only the less-than side moves the equals sign.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
1 == 1.0return?Answer
true, because==converts the number types==compares values and converts numbers, so the integer one and the float one point zero are equal. -
What does
1 =:= 1.0return?Answer
false, because the types differ=:=compares value and type, and an integer is not a float. -
Which operator means "not exactly equal"?
Answer
=/==/=is the negation of=:=, and/=is the negation of==. -
Which term sorts first?
Answer
1Numbers come before atoms, and atoms before tuples, maps and lists.
-
What does
"ab" < "abc"return?Answer
trueThe shorter list that is a prefix of the other sorts first.
-
How is less-than-or-equal written in Erlang?
Answer
=<Erlang writes
=<;<=is a syntax error before the character. -
What does
1 < areturn?Answer
trueThe term order puts every number before every atom, so the comparison is defined.
-
What does this module print?
-module(q1). -export([main/0]). main() -> io:format("~p~n", [1 == 1.0]), io:format("~p~n", [1 =:= 1.0]).Answer
truethenfalse==converts and finds the numbers equal;=:=keeps the types apart. -
What does this module print?
-module(q2). -export([main/0, max_of/2]). max_of(A, B) when A >= B -> A; max_of(_, B) -> B. main() -> io:format("~p~n", [max_of("apple", "banana")]).Answer
"banana"Strings compare by character codes, and
bananais greater thanapple. -
What does this module print?
-module(q3). -export([main/0]). main() -> io:format("~p~n", [1 =< 1]).Answer
true=<is the less-than-or-equal operator, and one is equal to one.
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