Meet six kinds of values: full examples and further reading
The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.
Run common values, then see why an Elixir string is a UTF-8 binary.
Run common values in IEx, compare :ok with "ok", and confirm that <<"Chang'an">> equals "Chang'an".
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" 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.
Steps for the full example
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".
Original lesson notes
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.
Symbols and terms
:- 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
trueorfalse. - 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
trueorfalse. It answers a yes-or-no question. - nil
- It means there is no value here. It is not the number
0or an empty string. - atom
- A label that begins with a colon, such as
:okor:red. Its name is its value. - string
- UTF-8 text inside double quotes, such as
"York". In Elixir, a string is a binary.
Original full exercise
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
____The three lines should produce "Bristol", :cloudy, and false.
Show hint
Put text in double quotes. Start a label with a colon. Write yes or no as true or false.
Show original answer
# A city is text
"Bristol"
# The weather is a label
:cloudy
# It is not raining today
falseWhy 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.
Original lesson takeaways
- First ask what a value is. Then decide how to use it.
:okis an atom."ok"is a string.- Strings hold UTF-8 text in binaries.
<<>>builds a binary directly.