Erlang basics · Lesson 02 (open contents)
Meet common terms
Learn numbers, atoms, tuples, lists, and maps.
Look at terms in erl
Look at each shape, then use an is_* function to check it.
%% Check numbers and atoms
{is_integer(12), is_float(12.5)}.
{is_atom(true), is_atom(ok)}.
%% Run a tuple and a list
{ok, 42}.
[peach, plum, apricot].
%% Run a map whose atom keys explain its values
#{status => ready, score => 92}.- The first two lines both return
{true, true}. - The next two lines show the tuple
{ok, 42}and the list[peach, plum, apricot]. - The final line shows a map with the keys
statusandscore.
Read from the first line down
is_integer(12)andis_float(12.5)each inspect one value and return true or false.trueis the atom commonly used to mean yes. Its type is still atom.#{...}creates a map. Herestatus,ready, andscoreare labels from a limited set.
Symbols are not secret signs
okIt begins with a lowercase letter, so it is an atom label.
{ok, 42}A two-item tuple. The first atom often labels the kind of result.
#{status => ready}A map. => joins a key to a value.
Match each name to its meaning
term
Erlang's general name for runtime data. Different terms have different shapes and jobs.
atom
A label usually written as a lowercase word, such as ok. Its name is its value.
tuple
A fixed group inside braces, such as {ok, 42}.
Why these forms are useful
Erlang calls every piece of runtime data a term. Numbers, atoms, tuples, lists, and maps are all terms.
A word that begins with a lowercase letter is usually an atom, such as ok or rain. In Erlang, true and false are atoms too.
Erlang has no built-in nil value. A project may choose undefined, none, or another atom to mean “missing,” but the project should state that rule.
Close the answer and try
Complete a weather map. Use an atom for the weather and an integer for the temperature.
%% Use weather and temperature as the keys
#{____ => cloudy, ____ => 28}.Target result: The result should be #{weather => cloudy, temperature => 28}.
Stuck? Read one hint
Fill the blanks with the atoms weather and temperature.
After you run it, see one answer
%% Atom keys explain what the two values mean
#{weather => cloudy, temperature => 28}.Think about it: Is `true` a separate type in Erlang?
No. true is an atom that people agree to use for a true result.
Remember these three lines
- 1
Erlang calls all runtime data terms.
- 2
Atoms are useful for a limited set of labels such as
ok,ready, andcloudy. - 3
Tuples and maps both hold several values, but maps give fields names.