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.

  1. What is an atom?

    Answer

    A constant whose name is its value

    An atom is a named constant: ok is the value ok, with no data inside it.

  2. Which of these is an atom written without quotes?

    Answer

    hello

    An unquoted atom starts with a lowercase letter; an uppercase name is a variable.

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

  4. What does 'Tuesday morning'. evaluate to?

    Answer

    The atom Tuesday morning

    Single quotes build an atom, so the space is allowed and the quotes are part of the notation, not the value.

  5. What does ok =:= ok. return?

    Answer

    true

    The strict equality operator compares both value and type, and the two atoms are identical.

  6. What is the difference between == and =:=?

    Answer

    == compares values loosely, =:= compares value and type

    1 == 1.0 is true, while 1 =:= 1.0 is false because an integer and a float are different types.

  7. What does 1 == 1.0. return?

    Answer

    true

    The loose equality converts the number types, so the integer one equals the float one.

  8. What does atom_to_list(ok). return?

    Answer

    "ok"

    atom_to_list returns the characters of the atom name as a string, and the shell prints a string in quotes.

  9. What does list_to_atom("ready"). return?

    Answer

    ready

    The function turns the characters into an atom, which the shell prints without quotes.

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