← Back to lesson 08: Let a function keep going

Let a function keep going: full examples and further reading

The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.

Stop at an empty list and use a head-and-tail pattern for the next item.

Run the recursive function with [1, 2, 3], confirm [2, 4, 6], and find the [] clause that stops it.

Copy the whole block into the erl shell

Read the [] clause first, then the [Head | Tail] clause. Do not try to follow every call at once.

Original full example
%% Clear variables left in the shell by earlier lessons
f().

%% Double stores the function; Loop is only for recursion inside it
Double =
  fun Loop([]) ->
        [];
      Loop([Head | Tail]) ->
        [Head * 2 | Loop(Tail)]
  end.

%% Call the function with its outside name
Double([1, 2, 3]).

Double([1, 2, 3]) returns [2, 4, 6].

The list becomes one item shorter with each call.

When the input reaches Loop([]), it returns [] and the recursion stops.

Steps for the full example

  1. For the input [1, 2, 3], Head is 1 and Tail is [2, 3].
  2. This layer prepares 1 * 2. The remaining work becomes Loop([2, 3]).
  3. Loop is visible only inside this fun. Code outside calls the whole function through the variable Double.
  4. The semicolon says another fun clause follows. The last clause goes straight to end..
  5. At last, the empty-list clause returns []. The results from the earlier layers are then joined back together.

Original lesson notes

Erlang often processes a list with recursion. A function handles the first item, then gives the remaining list to itself.

Recursion needs a stopping condition. For a list, the empty list [] is a natural finish line.

One function can have several clauses. Erlang searches from top to bottom for a clause with the right shape. Here, the first clause receives an empty list and the second receives [Head | Tail].

Symbols and terms
fun Loop([]) ->
Create an anonymous function with the internal name Loop. Its first clause accepts an empty list.
Loop([Head | Tail]) ->
The second clause splits the current item from the remaining items.
[Head * 2 | Loop(Tail)]
Put in the current result, then let Loop handle the shorter Tail.
recursion
A function calls itself inside its own definition.
function clause
One entry point for a function. Each clause accepts a different shape of argument.
stopping condition
A case that does not call the function again. This example returns when it reaches an empty list.

Original full exercise

Complete AddOne so that it adds 1 to every number in a list.

Exercise starter
%% Clear old variables so you can practice again
f().

%% Keep the stopping condition as it is
AddOne =
  fun Loop([]) ->
        [];
      Loop([Head | Tail]) ->
        [____ | Loop(Tail)]
  end.

%% Call the function and check its result
AddOne([2, 3, 4]).

AddOne([2, 3, 4]) should return [3, 4, 5].

Show hint

The new value for the current item is Head + 1.

Show original answer
Reference answer
%% f() belongs in the shell, not in an .erl file
f().

%% AddOne is the outside name; Loop is the inside recursion name
AddOne =
  fun Loop([]) ->
        [];
      Loop([Head | Tail]) ->
        [Head + 1 | Loop(Tail)]
  end.

%% This should return [3,4,5]
AddOne([2, 3, 4]).
Where does this recursion stop?

When the input becomes an empty list, Loop([]) -> [] returns directly and does not call itself again.

Original lesson takeaways
Design trade-offs and language comparisons
Original course references