Erlang basics · Lesson 14 (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 1425 minutes

Solve a list twice

Compare explicit recursion with `lists:foldl/3`.

01 · Start with the whole example

Run this first

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

Erlang
%% Compare recursion with a library fold
Sum = fun Loop([]) -> 0; Loop([Head | Tail]) -> Head + Loop(Tail) end,
Numbers = [1,2,3,4],
{Sum(Numbers), lists:foldl(fun(Number, Acc) -> Number + Acc end, 0, Numbers)}.
Check the result first
  • Both paths return 10, so the tuple is {10,10}.
02 · Take the code apart

Read from the first line down

  1. Read fun Sum([]) ->: Stop at the empty list.
  2. Read Sum([Head | Tail]): Handle one item and recurse on the tail.
  3. Read lists:foldl/3: Combine a list into one accumulator.
03 · Meet the new symbols

Symbols are not secret signs

fun Sum([]) ->

Stop at the empty list.

Sum([Head | Tail])

Handle one item and recurse on the tail.

lists:foldl/3

Combine a list into one accumulator.

04 · Ideas inside the code

Match each name to its meaning

01

base clause

A recursive function stops by matching an input such as the empty list.

02

recursive clause

The recursive clause handles one item and calls itself with a shorter tail.

03

fold

lists:foldl/3 carries an accumulator from left to right.

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.

A recursive function stops by matching an input such as the empty list.

The recursive clause handles one item and calls itself with a shorter tail.

06 · Change it yourself

Close the answer and try

Multiply [2,3,4] with lists:foldl/3.

Practice starting point
lists:foldl(fun(Number, Acc) -> ____ end, ____, [2,3,4]).

Target result: Return 24.

Stuck? Read one hint

Multiply Number by Acc and start at 1.

After you run it, see one answer
One answer
lists:foldl(fun(Number, Acc) -> Number * Acc end, 1, [2,3,4]).
Think about it: What stops the recursive fun?

Its Loop([]) -> 0 clause returns without another call.

Take with you

Remember these three lines

  1. 1

    Every recursion needs a base clause.

  2. 2

    Make each recursive input smaller.

  3. 3

    Use standard list folds for common accumulation.

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