Skip to content

Commit a48e56c

Browse files
author
José Valim
committed
Merge pull request elixir-lang#208 from augiedb/patch-1
Chapter 9 Edits for clarity
2 parents 699c263 + 16a1340 commit a48e56c

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

‎getting_started/9.markdown‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ for(i = 0; i < array.length; i++){
1414
}
1515
```
1616

17-
In the example above we are mutating the array which is not possible in Elixir. Instead functional languages rely on recursion: a function is called recursively until a condition is reached. Consider the example below that manually sums all the items in the list:
17+
In the example above, we are mutating the array. That's not possible in Elixir. Instead, functional languages rely on recursion: a function is called recursively until a condition is reached. Consider the example below that manually sums all the items in the list:
1818

1919
```elixir
2020
defmoduleMathdo
@@ -30,9 +30,9 @@ end
3030
Math.sum_list([1, 2, 3], 0) #=> 6
3131
```
3232

33-
In the example above, we invoke `sum_list`giving a list `[1,2,3]` and the initial value `0` as arguments. When a function has many clauses, we will try each clause until we find one that matches according to the pattern matching rules. In this case, the list `[1,2,3]` matches against `[h|t]` which assigns `h = 1` and `t = [2,3]` while `acc` is set to `0`.
33+
We invoke `sum_list`with a list `[1,2,3]` and the initial value `0` as arguments. When a function has many clauses, we will try each clause until we find one that matches according to the pattern matching rules. In this case, the list `[1,2,3]` matches against `[h|t]` which assigns `h = 1` and `t = [2,3]` while `acc` is set to `0`.
3434

35-
Then, we add the head of the list to the accumulator `h + acc` and call `sum_list` again, recursively, passing the tail of the list as argument. The tail will once again match `[h|t]` until the list is empty, as seen below:
35+
Then, we add the head of the list to the accumulator `h + acc` and call `sum_list` again, recursively, passing the tail of the list as its first argument. The tail will once again match `[h|t]` until the list is empty, as seen below:
3636

3737
```elixir
3838
sum_list [1, 2, 3], 0
@@ -50,7 +50,7 @@ iex> Enum.reduce([1, 2, 3], 0, fn(x, acc) -> x + acc end)
5050
6
5151
```
5252

53-
Or using the capture syntax:
53+
Or, using the capture syntax:
5454

5555
```iex
5656
iex> Enum.reduce([1, 2, 3], 0, &+/2)

0 commit comments

Comments
(0)