Elixir basics · Lesson 02 (open contents)
Meet six kinds of values
Run common values, then see why an Elixir string is a UTF-8 binary.
Meet values in IEx
Run one line at a time. The final line compares two ways to write the same UTF-8 binary.
# 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"- The first six parts produce an integer, a float, a boolean,
nil, an atom, and a string. :ok == "ok"returnsfalsebecause an atom and a string are different types.<<"Chang'an">> == "Chang'an"returnstruebecause both sides are the same UTF-8 binary.
Read from the first line down
12and12.5are both numbers. The decimal point separates integers from floats.trueandfalsestand for yes and no.nilmeans no value. Do not treat all three as one type.- A name after a colon is an atom. Text in double quotes is a string. Therefore
:okis not equal to"ok". <<"Chang'an">>writes a binary directly. A normal Elixir string is already a UTF-8 binary, so it equals"Chang'an".
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.
Match each name to its meaning
integer
A number without a decimal point, such as 12, 0, or -3.
float
A number with a decimal point, such as 12.5. For now, think of it as a number that can show a fraction.
boolean
Either true or false. It answers a yes-or-no question.
nil
It means there is no value here. It is not the number 0 or an empty string.
atom
A label that begins with a colon, such as :ok or :red. Its name is its value.
string
UTF-8 text inside double quotes, such as "York". In Elixir, a string is a binary.
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.
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.
# 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
# A city is text
"Bristol"
# The weather is a label
:cloudy
# It is not raining today
falseThink 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.
Remember these three lines
- 1
First ask what a value is. Then decide how to use it.
- 2
:okis an atom."ok"is a string. - 3
Strings hold UTF-8 text in binaries.
<<>>builds a binary directly.