Skip to content

Commit db0e336

Browse files
authored
Improvements to recursion guide
1 parent 5f59612 commit db0e336

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

lib/elixir/pages/getting-started/recursion.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ When the second argument is zero, the guard `n > 0` evaluates to false, and the
4646
Finally, if you pass an argument that does not match any clause, Elixir raises a `FunctionClauseError`:
4747

4848
```elixir
49-
iex> Recursion.print_multiple_times "Hello!", -1
49+
iex> Recursion.print_multiple_times("Hello!", -1)
5050
** (FunctionClauseError) no function clause matching in Recursion.print_multiple_times/2
5151

5252
The following arguments were given to Recursion.print_multiple_times/2:
@@ -83,10 +83,10 @@ We invoke `sum_list` with the list `[1, 2, 3]` and the initial value `0` as argu
8383
Then, we add the head of the list to the accumulator `head + accumulator` and call `sum_list` again, recursively, passing the tail of the list as its first argument. The tail will once again match `[head | tail]` until the list is empty, as seen below:
8484

8585
```elixir
86-
sum_list [1, 2, 3], 0
87-
sum_list [2, 3], 1
88-
sum_list [3], 3
89-
sum_list [], 6
86+
sum_list([1, 2, 3], 0)
87+
sum_list([2, 3], 1)
88+
sum_list([3], 3)
89+
sum_list([], 6)
9090
```
9191

9292
When the list is empty, it will match the final clause which returns the final result of `6`.
@@ -105,14 +105,8 @@ defmodule Math do
105105
[]
106106
end
107107
end
108-
```
109-
110-
```console
111-
$ iex math.exs
112-
```
113108

114-
```elixir
115-
iex> Math.double_each([1, 2, 3]) #=> [2, 4, 6]
109+
Math.double_each([1, 2, 3]) #=> [2, 4, 6]
116110
```
117111

118112
Here we have used recursion to traverse a list, doubling each element and returning a new list. The process of taking a list and _mapping_ over it is known as a _map algorithm_.

0 commit comments

Comments
 (0)