Erlang basics · Lesson 07 (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 0730 minutes

Choose one path with case

Match one value against branches, then add conditions with guards.

01 · Start with the whole example

Use case to see the order of branches

Change Score to 95 and 40, and run the code each time.

Erlang
%% Clear old variables so Score is easy to change and try again
f().

%% Try each branch from top to bottom
Score = 76.
Message =
  case Score of
    Value when Value >= 90 -> very_good;
    Value when Value >= 60 -> pass;
    _Other -> try_again
  end.

%% Look at the chosen atom
Message.
Check the result first
  • 76 produces the atom pass.
  • 95 produces very_good.
  • 40 produces try_again.
02 · Take the code apart

Read from the first line down

  1. case Score of says that the following branches will match Score.
  2. The first two branches end with semicolons because more branches follow. The final branch has no semicolon.
  3. The dot in end. ends the whole case expression.
03 · Meet the new symbols

Symbols are not secret signs

;

This case branch is finished, and another branch follows.

end.

End the whole case expression.

_Other

Match any value. The leading underscore says this variable will not be used again.

04 · Ideas inside the code

Match each name to its meaning

01

case branch

One rule for one shape of input. The first matching branch is chosen.

02

guard

An extra condition after when, such as when Score >= 60.

03

fallback

A final branch that handles the remaining inputs so an unexpected value is not left out.

05 · Make the idea clear

Why these forms are useful

case gets one value, then searches from top to bottom for the first matching branch.

Semicolons ; separate branches. Do not put a semicolon after the final branch. End the whole case with end..

A guard after when adds one simple condition to a pattern.

06 · Change it yourself

Close the answer and try

Choose an action for a weather atom: rain returns umbrella, sunny returns hat, and anything else returns look_up.

Practice starting point
%% Clear Score, Message, and any old Weather value
f().

%% Try other atoms too
Weather = rain.

case Weather of
  ____ -> umbrella;
  ____ -> hat;
  ____ -> look_up
end.

Target result: rain produces umbrella, and sunny produces hat.

Stuck? Read one hint

Use fixed atoms in the first two blanks. Use _Other to catch every other value.

After you run it, see one answer
One answer
%% f() is used only in the shell
f().

%% Weather is the atom to match
Weather = rain.

case Weather of
  rain -> umbrella;
  sunny -> hat;
  _Other -> look_up
end.
Think about it: Should the final `case` branch end with a semicolon?

No. A semicolon means that another branch follows. The final branch goes straight to end..

Take with you

Remember these three lines

  1. 1

    case chooses the first matching branch from top to bottom.

  2. 2

    The order of patterns can change the result.

  3. 3

    Semicolons separate branches. end. closes the whole case.

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