Erlang basics · Lesson 02 (open contents)
01 · Open erl02 · Meet common terms03 · Tell two kinds of text apart04 · Bind a variable once05 · Split and filter a list06 · Parse a number safely07 · Choose one path with case08 · Let a function keep going09 · Put code in a module10 · Compare terms and Boolean results11 · Read and update nested maps12 · Build and inspect UTF-8 binaries13 · Let clauses choose the function path14 · Solve a list twice15 · Choose with patterns, guards, and errors16 · Describe a module and one record17 · Put one tested module in Rebar318 · Return success or failure as data19 · Read and write one file safely20 · Build a data pipeline with functions21 · Keep records behind a module boundary22 · Define a module contract23 · Describe the data contract24 · Test small rules and whole flows25 · Shape builds and make a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ERLANG · SCRATCH · LESSON 0225 minutes

Meet common terms

Learn numbers, atoms, tuples, lists, and maps.

01 · Start with the whole example

Look at terms in erl

Look at each shape, then use an is_* function to check it.

Erlang
%% 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}.
Check the result first
  • 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 status and score.
02 · Take the code apart

Read from the first line down

  1. is_integer(12) and is_float(12.5) each inspect one value and return true or false.
  2. true is the atom commonly used to mean yes. Its type is still atom.
  3. #{...} creates a map. Here status, ready, and score are labels from a limited set.
03 · Meet the new symbols

Symbols are not secret signs

ok

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

04 · Ideas inside the code

Match each name to its meaning

01

term

Erlang's general name for runtime data. Different terms have different shapes and jobs.

02

atom

A label usually written as a lowercase word, such as ok. Its name is its value.

03

tuple

A fixed group inside braces, such as {ok, 42}.

05 · Make the idea clear

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.

06 · Change it yourself

Close the answer and try

Complete a weather map. Use an atom for the weather and an integer for the temperature.

Practice starting point
%% 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
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.

Take with you

Remember these three lines

  1. 1

    Erlang calls all runtime data terms.

  2. 2

    Atoms are useful for a limited set of labels such as ok, ready, and cloudy.

  3. 3

    Tuples and maps both hold several values, but maps give fields names.

Lesson completeRun the code once, then mark this lesson as done.