Atoms
What atoms are
An atom is a constant whose name is its value. Atoms carry no data of their own; they mark states, results, and keys.
The two most famous atoms are true and false. The empty list [] is the other value that counts as false, and lesson 9 covers it.
erl
1> ok.
ok
2> error.
error
Atom rules
An atom starts with a lowercase letter and continues with letters, digits, underscores, and the at sign. Anything richer needs single quotes, and the quotes become part of the atom.
Atoms are not garbage collected in the old sense: the atom table is global and bounded, so never build them from user input in a loop.
erl
1> max_value.
max_value
2> 'Tuesday morning'.
'Tuesday morning'
Compare atoms
The double equals compares values loosely, and the triple equals compares types strictly. Atoms never equal numbers, even when they look alike.
Use the strict form by default: it keeps 1 and 1.0 apart and makes every test predictable.
erl
1> ok =:= ok.
true
2> ok == ok.
true
3> 1 == 1.0.
true
Atom tools
The atom module converts between atoms and strings. list_to_atom builds an atom at runtime, which is the one place to be careful with the atom table.
erl
1> atom_to_list(ok).
"ok"
2> list_to_atom("ready").
ready
Next: Strings and binaries
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is an atom?
Answer
A constant whose name is its value
An atom is a named constant:
okis the valueok, with no data inside it. -
Which of these is an atom written without quotes?
Answer
helloAn unquoted atom starts with a lowercase letter; an uppercase name is a variable.
-
When does an atom need single quotes?
Answer
When it starts with a capital or holds spaces or punctuation
The plain syntax allows lowercase letters, digits,
_and@; anything else needs quotes. -
What does
'Tuesday morning'.evaluate to?Answer
The atom
Tuesday morningSingle quotes build an atom, so the space is allowed and the quotes are part of the notation, not the value.
-
What does
ok =:= ok.return?Answer
trueThe strict equality operator compares both value and type, and the two atoms are identical.
-
What is the difference between
==and=:=?Answer
==compares values loosely,=:=compares value and type1 == 1.0is true, while1 =:= 1.0is false because an integer and a float are different types. -
What does
1 == 1.0.return?Answer
trueThe loose equality converts the number types, so the integer one equals the float one.
-
What does
atom_to_list(ok).return?Answer
"ok"atom_to_listreturns the characters of the atom name as a string, and the shell prints a string in quotes. -
What does
list_to_atom("ready").return?Answer
readyThe function turns the characters into an atom, which the shell prints without quotes.
-
Why is building atoms from user input in a loop dangerous?
Answer
The atom table is global and bounded
Every new atom stays in the global table, so unbounded input can exhaust it and stop the node.
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