Elixir basics · Lesson 02 (open contents)
01 · Let the code speak02 · Meet six kinds of values03 · Put values together04 · Build a new list05 · Make the shapes match06 · Choose with case07 · Parse a number safely08 · Unpack &1 and the pipe09 · Put code in a project10 · Ask precise true-or-false questions11 · Read and update nested data12 · Count visible text and bytes13 · Let function clauses choose14 · Solve a list twice15 · Choose the smallest clear control form16 · Give domain data a shape17 · Put one tested module in Mix18 · Make success and failure predictable19 · Read and write one real file20 · Take only the data you need21 · Give different data one shared action22 · Write a callback contract23 · Write down the data contract24 · Test behavior and boundaries25 · Turn a project into 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
ELIXIR · SCRATCH · LESSON 0220 minutes

Meet six kinds of values

Run common values, then see why an Elixir string is a UTF-8 binary.

01 · Start with the whole example

Meet values in IEx

Run one line at a time. The final line compares two ways to write the same UTF-8 binary.

Elixir
# Run one value of each common type
12
12.5
true
nil
:ok
"Hello, Robin"

# Compare an atom, a string, and a directly written binary
:ok == "ok"
<<"Chang'an">> == "Chang'an"
Check the result first
  • The first six parts produce an integer, a float, a boolean, nil, an atom, and a string.
  • :ok == "ok" returns false because an atom and a string are different types.
  • <<"Chang'an">> == "Chang'an" returns true because both sides are the same UTF-8 binary.
02 · Take the code apart

Read from the first line down

  1. 12 and 12.5 are both numbers. The decimal point separates integers from floats.
  2. true and false stand for yes and no. nil means no value. Do not treat all three as one type.
  3. A name after a colon is an atom. Text in double quotes is a string. Therefore :ok is not equal to "ok".
  4. <<"Chang'an">> writes a binary directly. A normal Elixir string is already a UTF-8 binary, so it equals "Chang'an".
03 · Meet the new symbols

Symbols are not secret signs

:

Put it before a name to make an atom, for example :ok.

" "

Double quotes wrap text to make a string.

<< >>

Double angle brackets build a binary.

==

Compare the values on both sides. The result is true or false.

04 · Ideas inside the code

Match each name to its meaning

01

integer

A number without a decimal point, such as 12, 0, or -3.

02

float

A number with a decimal point, such as 12.5. For now, think of it as a number that can show a fraction.

03

boolean

Either true or false. It answers a yes-or-no question.

04

nil

It means there is no value here. It is not the number 0 or an empty string.

05

atom

A label that begins with a colon, such as :ok or :red. Its name is its value.

06

string

UTF-8 text inside double quotes, such as "York". In Elixir, a string is a binary.

05 · Make the idea clear

Why these forms are useful

A value is something the code is working with. An age can be an integer. A height can be a decimal number. A name can be a string.

A type tells us which operations a value can join. Numbers can be added. Strings can be joined. A different type is not better or worse. It simply has a different job.

:ok is an atom, often used as a label. "ok" is a string made of two letters. They look alike, but they are different kinds of values.

An Elixir string stores UTF-8 text in a binary. Double quotes are the usual way to write one. The <<>> form builds a binary directly, so <<"Chang'an">> and "Chang'an" contain the same bytes.

06 · Change it yourself

Close the answer and try

Write a city, the weather, and whether it is raining on three lines. Write only values for now. Do not give them names.

Practice starting point
# A city is text
____
# The weather is a label
____
# It is not raining today
____

Target result: The three lines should produce "Bristol", :cloudy, and false.

Stuck? Read one hint

Put text in double quotes. Start a label with a colon. Write yes or no as true or false.

After you run it, see one answer
One answer
# A city is text
"Bristol"
# The weather is a label
:cloudy
# It is not raining today
false
Think about it: Why does `<<"Chang'an">> == "Chang'an"` return `true`?

An Elixir string is a UTF-8 binary. Both expressions contain the same bytes, even though they use different syntax.

Take with you

Remember these three lines

  1. 1

    First ask what a value is. Then decide how to use it.

  2. 2

    :ok is an atom. "ok" is a string.

  3. 3

    Strings hold UTF-8 text in binaries. <<>> builds a binary directly.

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