Elixir basics · Lesson 01 (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 0115 minutes

Let the code speak

Open IEx, enter an expression, and tell code, results, and errors apart.

01 · Start with the whole example

Copy into IEx and press Enter after each line

Run one line at a time. Guess the result before you press Enter.

Elixir
# An addition expression returns a number
2 + 3

# A function call returns new text
String.upcase("cat")

# IO.puts prints a line on the screen
IO.puts("Hello, BEAM")
Check the result first
  • 2 + 3 returns 5.
  • String.upcase("cat") returns "CAT".
  • IO.puts("Hello, BEAM") prints the text, then IEx shows :ok. For now, read it as a “done” label.
02 · Take the code apart

Read from the first line down

  1. In 2 + 3, + adds the numbers on its left and right.
  2. String.upcase("cat") gives the string to the upcase function in the String module.
  3. IO.puts("Hello, BEAM") creates two visible results: the printed text and the return value :ok shown by IEx.
03 · Meet the new symbols

Symbols are not secret signs

iex

Enter this in a terminal to open IEx.

iex>

This is the prompt. Enter only the code after it. Do not copy the prompt.

#

This line is a comment. Elixir does not run it.

04 · Ideas inside the code

Match each name to its meaning

01

IEx

Short for Interactive Elixir. It is like scratch paper for trying one or two lines of code.

02

expression

Code that produces a value when it runs. A number, an operation, or a function call can all be expressions.

03

return value

The result an expression gives back when it finishes. It does not always print a sentence.

05 · Make the idea clear

Why these forms are useful

IEx is Elixir's interactive window. You enter a small piece of code, and it answers at once. You do not need a project or a long list of commands yet.

An expression is code that produces a result. 2 + 3 produces 5. String.upcase("cat") produces "CAT".

When you see an error, pause. Read the last few lines. Look for a name the computer does not know or data that does not fit. An error is not a lost point. It is another kind of result.

06 · Change it yourself

Close the answer and try

Ask IEx to multiply 7 by 6, then turn beam into uppercase text.

Practice starting point
# Replace the question mark with an operator
7 ? 6

# Finish the function name
String.____("beam")

Target result: First you should see 42, then "BEAM".

Stuck? Read one hint

Multiplication uses *. The function that made text uppercase was upcase.

After you run it, see one answer
One answer
# The star means multiplication
7 * 6

# upcase returns a new uppercase string
String.upcase("beam")
Think about it: Is `iex>` part of the code?

No. It is the prompt IEx shows while it waits for your code.

Take with you

Remember these three lines

  1. 1

    IEx is useful for trying very short Elixir code.

  2. 2

    An expression produces a value when it runs.

  3. 3

    Read what an error says, then change only one thing.

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