Erlang basics · Lesson 13 (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 1325 minutes

Let clauses choose the function path

Write named functions with patterns and guards in a module.

01 · Start with the whole example

Run this first

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

Erlang
%% Save in fare.erl and compile it
-module(fare).
-export([price/1]).
price(Age) when Age < 12 -> 5;
price(_Age) -> 10.
Check the result first
  • After compiling, {fare:price(10), fare:price(20)}. returns {5,10}.
02 · Take the code apart

Read from the first line down

  1. Read price(Age) when Age < 12: Choose this clause for a child age.
  2. Read ;: Separate clauses of the same function.
  3. Read .: End the final clause of the function.
03 · Meet the new symbols

Symbols are not secret signs

price(Age) when Age < 12

Choose this clause for a child age.

;

Separate clauses of the same function.

.

End the final clause of the function.

04 · Ideas inside the code

Match each name to its meaning

01

function clause

Clauses with the same name and arity can accept different patterns or guards.

02

guard

A guard refines a clause with permitted checks after when.

03

clause order

Erlang tries matching clauses from top to bottom.

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.

Clauses with the same name and arity can accept different patterns or guards.

A guard refines a clause with permitted checks after when.

06 · Change it yourself

Close the answer and try

Write label/1 clauses for zero and positive integers.

Practice starting point
label(0) -> ____;
label(Number) when Number > 0 -> ____.

Target result: Return zero and positive.

Stuck? Read one hint

Use atoms as the two results.

After you run it, see one answer
One answer
label(0) -> zero;
label(Number) when Number > 0 -> positive.
Think about it: Why does the first clause end with a semicolon?

Another clause of the same function follows; the final clause ends with a dot.

Take with you

Remember these three lines

  1. 1

    Name and arity identify a function.

  2. 2

    Patterns and guards select clauses.

  3. 3

    Put specific clauses before broad fallbacks.

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