Erlang basics · Lesson 15 (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 · FOUNDATION · LESSON 1525 minutes

Choose with patterns, guards, and errors

Use `case`, `if`, and `try` for three different decisions.

01 · Start with the whole example

Run this first

Run it once, change one input, and compare the new result.

Erlang
%% Use each form for the decision it fits
Score = 76,
Label = if Score >= 90 -> great; Score >= 60 -> pass; true -> retry end,
Parsed = case string:to_integer("42") of {N, []} -> {ok, N}; _ -> {error, bad_text} end,
Safe = try binary_to_integer(<<"x">>) catch error:badarg -> {error, badarg} end,
{Label, Parsed, Safe}.
Check the result first
  • The result is {pass,{ok,42},{error,badarg}}.
02 · Take the code apart

Read from the first line down

  1. Read case Value of: Match one value against branches.
  2. Read if Guard ->: Choose by guard conditions.
  3. Read catch error:Reason ->: Handle a named error reason.
03 · Meet the new symbols

Symbols are not secret signs

case Value of

Match one value against branches.

if Guard ->

Choose by guard conditions.

catch error:Reason ->

Handle a named error reason.

04 · Ideas inside the code

Match each name to its meaning

01

case

case matches one value against ordered patterns.

02

if expression

Erlang if chooses the first guard expression that is true.

03

try expression

try separates normal results from expected error classes and reasons.

05 · Make the idea clear

Why these forms are useful

Run the example first. Predict one result, then change one input and run it again.

case matches one value against ordered patterns.

Erlang if chooses the first guard expression that is true.

06 · Change it yourself

Close the answer and try

Use case to turn {error, timeout} into retry.

Practice starting point
case {error, timeout} of
  {ok, Value} -> Value;
  {error, ____} -> ____
end.

Target result: Return retry.

Stuck? Read one hint

Match timeout and return the atom retry.

After you run it, see one answer
One answer
case {error, timeout} of
  {ok, Value} -> Value;
  {error, timeout} -> retry
end.
Think about it: Is Erlang `if` a general else-if statement?

It is a sequence of guard clauses; use case when patterns describe the data more clearly.

Take with you

Remember these three lines

  1. 1

    Use case for shapes.

  2. 2

    Use if for guard-only choices.

  3. 3

    Catch only errors the boundary expects.

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