Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions chapters/_subfiles/functional-programming/_adder_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
::: {#exm-adder}

Check warning on line 1 in chapters/_subfiles/functional-programming/_adder_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-adder has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### A function that returns a function

The function `adder` takes a number `d` and returns a function:

```racket
```{racket}
#| code-fold: false
(define (adder d)
(lambda (x) (+ x d)))
```
Expand All @@ -12,8 +13,15 @@
which still knows that `d` is 3 after `adder` has returned.
So `adder` is a higher-order function (@def-higher-order), and its value can be used anywhere a function can:

- `((adder 3) 4)` returns 7.
- `(twice (adder 3) 10)`, with `twice` from @exr-twice, returns 16.
- `(map (adder 1) (list 5 7 9))` returns `'(6 8 10)`.
- applied to an argument;
- passed to `twice` from @exr-twice;
- passed to `map` from @exm-map-filter.

```{racket}
#| code-fold: false
((adder 3) 4)
(twice (adder 3) 10)
(map (adder 1) (list 5 7 9))
```

:::
18 changes: 15 additions & 3 deletions chapters/_subfiles/functional-programming/_map-filter_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
::: {#exm-map-filter}

Check warning on line 1 in chapters/_subfiles/functional-programming/_map-filter_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-map-filter has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### `map` and `filter`

Racket's `(list 5 7 9)` makes the list of 5, 7 and 9, which Racket prints as `'(5 7 9)`.
Racket's `(list 5 7 9)` makes the list of 5, 7 and 9, which Racket prints with a quote mark in front:

```{racket}
#| code-fold: false
(list 5 7 9)
```

Two built-in higher-order functions (@def-higher-order) work on lists:

- `(map f L)` applies `f` to each element of the list `L`, and returns the list of results.
With `add-three` from @exr-twice, `(map add-three (list 5 7 9))` returns `'(8 10 12)`.
- `(filter p L)` returns the list of the elements `x` of `L` for which `(p x)` is true.
`(filter even? (list 5 6 7 8))` returns `'(6 8)`.

Applied to `add-three` from @exr-twice and to Racket's test `even?`, they return:

```{racket}
#| code-fold: false
(map add-three (list 5 7 9))
(filter even? (list 5 6 7 8))
```

Neither one changes `L`; each returns a new list.

Expand Down
3 changes: 2 additions & 1 deletion chapters/_subfiles/functional-programming/_pair-with_exr.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

The function `pair-with` takes a list `L` and a value `y`:

```racket
```{racket}
#| code-fold: false
(define (pair-with L y)
(map (lambda (x) (list x y)) L))
```
Expand Down
9 changes: 9 additions & 0 deletions chapters/_subfiles/functional-programming/_pair-with_sol.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ written in place without a name.
so `map` applies the function $x \mapsto (x, \text{"a"})$ to each element,
and returns `'((1 "a") (2 "a") (3 "a"))`.

Racket returns the same values:

```{racket}
#| code-fold: false
((lambda (x) (* x x)) 7)
(map (lambda (x) (* 2 x)) (list 1 2 3))
(pair-with (list 1 2 3) "a")
```

:::
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
::: {#exm-quadratic-roots}

Check warning on line 1 in chapters/_subfiles/functional-programming/_quadratic-roots_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-quadratic-roots has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### Naming a value used twice

The solutions of $a x^2 + b x + c = 0$ with $a \ne 0$ and $b^2 - 4ac \ge 0$ are
Expand All @@ -7,14 +7,20 @@
$$
Both solutions use $s$, so a `let` names it and computes it once:

```racket
```{racket}
#| code-fold: false
(define (roots a b c)
(let ([s (sqrt (- (* b b) (* 4 a c)))])
(list (/ (+ (- b) s) (* 2 a))
(/ (- (- b) s) (* 2 a)))))
```

`(roots 1 -3 2)` returns `'(2 1)`, since $x^2 - 3x + 2 = (x - 1)(x - 2)$.
Since $x^2 - 3x + 2 = (x - 1)(x - 2)$, the roots of $x^2 - 3x + 2$ are 2 and 1:

```{racket}
#| code-fold: false
(roots 1 -3 2)
```

`let` does not change a variable: inside the body, `s` stands for one value, as a name does in algebra.
So `roots` is a pure function (@def-pure-function).
Expand Down
8 changes: 7 additions & 1 deletion chapters/_subfiles/functional-programming/_spread_exr.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
#### Applying a function to a list

`(apply f L)` applies the function `f` to the elements of the list `L` as its arguments,
so `(apply + (list 2 7 6))` is the same as `(+ 2 7 6)`, which returns 15.
so `(apply + (list 2 7 6))` is the same as `(+ 2 7 6)`:

```{racket}
#| code-fold: false
(apply + (list 2 7 6))
(+ 2 7 6)
```

1. Guess what `(apply max (list 3 9 2))` returns.
2. Use `apply`, `max` and `min` to define `(spread L)`,
Expand Down
11 changes: 10 additions & 1 deletion chapters/_subfiles/functional-programming/_spread_sol.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
1. `(apply max (list 3 9 2))` is `(max 3 9 2)`, which returns 9.
2. One answer:

```racket
```{racket}
#| code-fold: false
(define (spread L)
(- (apply max L) (apply min L)))
```
Expand All @@ -12,4 +13,12 @@

`spread` needs no recursion: `max` and `min` already take any number of arguments, and `apply` hands each of them the whole list.

Racket returns the same values:

```{racket}
#| code-fold: false
(apply max (list 3 9 2))
(spread (list 4 -1 7 2))
```

:::
Loading