From 90bcb2a996f5bc5db055a263e87db44ba79fd210 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 10:11:14 +0000 Subject: [PATCH 1/3] Run the second half of the functional-programming chapter's Racket examples Continues the first half: map and filter, spread, pair-with, adder and roots now run as {racket} chunks, and their typed return values are chunk output (#131). The solutions keep their worked reasoning and end with a chunk that runs the same expressions. The lambda and let syntax templates stay plain fences. Part of #131. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FWgYrEUFVHj2qKjKncHg77 --- .../functional-programming/_adder_exm.qmd | 19 ++++++++++++++----- .../_map-filter_exm.qmd | 18 +++++++++++++++--- .../functional-programming/_pair-with_exr.qmd | 3 ++- .../functional-programming/_pair-with_sol.qmd | 9 +++++++++ .../_quadratic-roots_exm.qmd | 10 ++++++++-- .../functional-programming/_spread_exr.qmd | 8 +++++++- .../functional-programming/_spread_sol.qmd | 11 ++++++++++- 7 files changed, 65 insertions(+), 13 deletions(-) diff --git a/chapters/_subfiles/functional-programming/_adder_exm.qmd b/chapters/_subfiles/functional-programming/_adder_exm.qmd index 30c334dbe..adcc35caf 100644 --- a/chapters/_subfiles/functional-programming/_adder_exm.qmd +++ b/chapters/_subfiles/functional-programming/_adder_exm.qmd @@ -3,17 +3,26 @@ The function `adder` takes a number `d` and returns a function: -```racket +```{racket} +#| code-fold: false (define (adder d) (lambda (x) (+ x d))) ``` `(adder 3)` returns the function $x \mapsto x + 3$, 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: +So `adder` is a higher-order function (@def-higher-order), and its value can be used anywhere a function can. +The three calls below use it in three ways: -- `((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` (@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 8c8176e38..745e1fe04 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 86484b964..ef80cc6d3 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 2503d6136..98f0850f2 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 41942d101..62edee747 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 06df9d17c..1b14aed6d 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 5eb559103..039f77d7f 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)) +``` + ::: From 48efa069a8ec5b54654017621cf8ae1f4ad35246 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 10:26:47 +0000 Subject: [PATCH 2/3] Keep exm-adder's lead-in line as it was The rewritten line kept its trailing (@def-higher-order) crossref, which check-avoided-words flags on a changed line. Restore the line, and make the bullets describe the three uses the chunk runs; the map bullet links its word instead of a trailing crossref. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FWgYrEUFVHj2qKjKncHg77 --- chapters/_subfiles/functional-programming/_adder_exm.qmd | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/chapters/_subfiles/functional-programming/_adder_exm.qmd b/chapters/_subfiles/functional-programming/_adder_exm.qmd index adcc35caf..c86e6ed89 100644 --- a/chapters/_subfiles/functional-programming/_adder_exm.qmd +++ b/chapters/_subfiles/functional-programming/_adder_exm.qmd @@ -11,12 +11,11 @@ The function `adder` takes a number `d` and returns a function: `(adder 3)` returns the function $x \mapsto x + 3$, 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. -The three calls below use it in three ways: +So `adder` is a higher-order function (@def-higher-order), and its value can be used anywhere a function can: - applied to an argument; - passed to `twice` from @exr-twice; -- passed to `map` (@exm-map-filter). +- passed to [`map`](#exm-map-filter). ```{racket} #| code-fold: false From c0519f6120cd6f13a96be4c25989b95dbaafc939 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 09:10:42 +0000 Subject: [PATCH 3/3] Cite the map example the way the bullet above cites twice The adder example's list cited @exr-twice in one bullet and linked [`map`](#exm-map-filter) in the next. Use a cross-reference in both, so the second reads "Example 3" like the first. Raised by the cross-harness read of #141. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FWgYrEUFVHj2qKjKncHg77 --- chapters/_subfiles/functional-programming/_adder_exm.qmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapters/_subfiles/functional-programming/_adder_exm.qmd b/chapters/_subfiles/functional-programming/_adder_exm.qmd index c86e6ed89..4a972bb25 100644 --- a/chapters/_subfiles/functional-programming/_adder_exm.qmd +++ b/chapters/_subfiles/functional-programming/_adder_exm.qmd @@ -15,7 +15,7 @@ So `adder` is a higher-order function (@def-higher-order), and its value can be - applied to an argument; - passed to `twice` from @exr-twice; -- passed to [`map`](#exm-map-filter). +- passed to `map` from @exm-map-filter. ```{racket} #| code-fold: false