diff --git a/chapters/_subfiles/functional-programming/_adder_exm.qmd b/chapters/_subfiles/functional-programming/_adder_exm.qmd index 30c334db..4a972bb2 100644 --- a/chapters/_subfiles/functional-programming/_adder_exm.qmd +++ b/chapters/_subfiles/functional-programming/_adder_exm.qmd @@ -3,7 +3,8 @@ The function `adder` takes a number `d` and returns a function: -```racket +```{racket} +#| code-fold: false (define (adder d) (lambda (x) (+ x d))) ``` @@ -12,8 +13,15 @@ The function `adder` takes a number `d` and returns a function: 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)) +``` ::: diff --git a/chapters/_subfiles/functional-programming/_map-filter_exm.qmd b/chapters/_subfiles/functional-programming/_map-filter_exm.qmd index 8c8176e3..745e1fe0 100644 --- a/chapters/_subfiles/functional-programming/_map-filter_exm.qmd +++ b/chapters/_subfiles/functional-programming/_map-filter_exm.qmd @@ -1,13 +1,25 @@ ::: {#exm-map-filter} #### `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. diff --git a/chapters/_subfiles/functional-programming/_pair-with_exr.qmd b/chapters/_subfiles/functional-programming/_pair-with_exr.qmd index 86484b96..ef80cc6d 100644 --- a/chapters/_subfiles/functional-programming/_pair-with_exr.qmd +++ b/chapters/_subfiles/functional-programming/_pair-with_exr.qmd @@ -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)) ``` diff --git a/chapters/_subfiles/functional-programming/_pair-with_sol.qmd b/chapters/_subfiles/functional-programming/_pair-with_sol.qmd index 2503d613..98f0850f 100644 --- a/chapters/_subfiles/functional-programming/_pair-with_sol.qmd +++ b/chapters/_subfiles/functional-programming/_pair-with_sol.qmd @@ -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") +``` + ::: diff --git a/chapters/_subfiles/functional-programming/_quadratic-roots_exm.qmd b/chapters/_subfiles/functional-programming/_quadratic-roots_exm.qmd index 41942d10..62edee74 100644 --- a/chapters/_subfiles/functional-programming/_quadratic-roots_exm.qmd +++ b/chapters/_subfiles/functional-programming/_quadratic-roots_exm.qmd @@ -7,14 +7,20 @@ x = \frac{-b \pm s}{2a}, \qquad \text{where } s = \sqrt{b^2 - 4ac}. $$ 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). diff --git a/chapters/_subfiles/functional-programming/_spread_exr.qmd b/chapters/_subfiles/functional-programming/_spread_exr.qmd index 06df9d17..1b14aed6 100644 --- a/chapters/_subfiles/functional-programming/_spread_exr.qmd +++ b/chapters/_subfiles/functional-programming/_spread_exr.qmd @@ -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)`, diff --git a/chapters/_subfiles/functional-programming/_spread_sol.qmd b/chapters/_subfiles/functional-programming/_spread_sol.qmd index 5eb55910..039f77d7 100644 --- a/chapters/_subfiles/functional-programming/_spread_sol.qmd +++ b/chapters/_subfiles/functional-programming/_spread_sol.qmd @@ -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))) ``` @@ -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)) +``` + :::