From f60dc28d00afc8938c1ce4f6d32c02bf135d767c Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 10:11:14 +0000 Subject: [PATCH 1/2] Run the first half of the functional-programming chapter's Racket examples The chapter typed what each Racket call returns, so nothing checked the values against the code (#131). In the first half of the chapter, up to exr-twice, each fence that defines a function is now a {racket} chunk, and each typed value in an example is a chunk's output instead. The solutions keep their worked reasoning and end with a chunk that runs the same expressions, so a wrong hand calculation shows up beside the right value. The syntax templates in the definitions of define and cond are not runnable, and the evaluation traces are hand rewriting steps, so they stay plain fences. Part of #131. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FWgYrEUFVHj2qKjKncHg77 --- .../functional-programming/_absolute-value_exm.qmd | 12 ++++++++++-- .../functional-programming/_evaluate-nested_sol.qmd | 10 ++++++++++ .../_formula-from-code_exr.qmd | 3 ++- .../_formula-from-code_sol.qmd | 8 ++++++++ .../functional-programming/_ticket-price_exr.qmd | 3 ++- .../functional-programming/_ticket-price_sol.qmd | 10 ++++++++++ .../_subfiles/functional-programming/_twice_exr.qmd | 3 ++- .../_subfiles/functional-programming/_twice_sol.qmd | 9 +++++++++ chapters/functional-programming.qmd | 2 ++ 9 files changed, 55 insertions(+), 5 deletions(-) diff --git a/chapters/_subfiles/functional-programming/_absolute-value_exm.qmd b/chapters/_subfiles/functional-programming/_absolute-value_exm.qmd index 3901f33f8..65b73beb4 100644 --- a/chapters/_subfiles/functional-programming/_absolute-value_exm.qmd +++ b/chapters/_subfiles/functional-programming/_absolute-value_exm.qmd @@ -11,14 +11,22 @@ x & \text{otherwise} $$ has two cases, so one `if` (@def-if-cond) is enough: -```racket +```{racket} +#| code-fold: false (define (absolute x) (if (< x 0) (- x) x)) ``` -`(absolute -7)` returns 7, and `(absolute 2)` returns 2. +Applied to $-7$ and to 2, it returns their absolute values: + +```{racket} +#| code-fold: false +(absolute -7) +(absolute 2) +``` + Racket already has this function, under the name `abs`. ::: diff --git a/chapters/_subfiles/functional-programming/_evaluate-nested_sol.qmd b/chapters/_subfiles/functional-programming/_evaluate-nested_sol.qmd index 2831478b9..98e98d0a7 100644 --- a/chapters/_subfiles/functional-programming/_evaluate-nested_sol.qmd +++ b/chapters/_subfiles/functional-programming/_evaluate-nested_sol.qmd @@ -9,4 +9,14 @@ Work from the innermost parentheses outward. The expression $(2 + 3) \cdot 4 - 1$ subtracts 1 from the product of $2 + 3$ and 4, so in Racket it is `(- (* (+ 2 3) 4) 1)`, which returns 19. +Racket returns the same values: + +```{racket} +#| code-fold: false +(* (+ 1 2) (- 10 4)) +(- 20 (* 3 (+ 1 1))) +(+ 1 2 3 4) +(- (* (+ 2 3) 4) 1) +``` + ::: diff --git a/chapters/_subfiles/functional-programming/_formula-from-code_exr.qmd b/chapters/_subfiles/functional-programming/_formula-from-code_exr.qmd index 3ff014c8e..3da90ce51 100644 --- a/chapters/_subfiles/functional-programming/_formula-from-code_exr.qmd +++ b/chapters/_subfiles/functional-programming/_formula-from-code_exr.qmd @@ -3,7 +3,8 @@ This Racket code defines a function named `h`: -```racket +```{racket} +#| code-fold: false (define (h x) (- (* x x) (* 2 x))) ``` diff --git a/chapters/_subfiles/functional-programming/_formula-from-code_sol.qmd b/chapters/_subfiles/functional-programming/_formula-from-code_sol.qmd index 04a6439f1..6367cbb5b 100644 --- a/chapters/_subfiles/functional-programming/_formula-from-code_sol.qmd +++ b/chapters/_subfiles/functional-programming/_formula-from-code_sol.qmd @@ -5,4 +5,12 @@ 3. Racket first evaluates the argument `(h 3)`, which is $9 - 6 = 3$, and then applies `h` to it, so `(h (h 3))` is `(h 3)` again and returns 3. +Racket returns the same values: + +```{racket} +#| code-fold: false +(h 5) +(h (h 3)) +``` + ::: diff --git a/chapters/_subfiles/functional-programming/_ticket-price_exr.qmd b/chapters/_subfiles/functional-programming/_ticket-price_exr.qmd index fafecc118..a70e88665 100644 --- a/chapters/_subfiles/functional-programming/_ticket-price_exr.qmd +++ b/chapters/_subfiles/functional-programming/_ticket-price_exr.qmd @@ -3,7 +3,8 @@ A museum sets its ticket price, in dollars, by age: -```racket +```{racket} +#| code-fold: false (define (ticket-price age) (cond [(< age 5) 0] [(< age 18) 8] diff --git a/chapters/_subfiles/functional-programming/_ticket-price_sol.qmd b/chapters/_subfiles/functional-programming/_ticket-price_sol.qmd index d5ffb9b1f..ed5ee43e8 100644 --- a/chapters/_subfiles/functional-programming/_ticket-price_sol.qmd +++ b/chapters/_subfiles/functional-programming/_ticket-price_sol.qmd @@ -19,4 +19,14 @@ \end{cases} $$ +Racket returns the same values: + +```{racket} +#| code-fold: false +(ticket-price 4) +(ticket-price 5) +(ticket-price 17) +(ticket-price 70) +``` + ::: diff --git a/chapters/_subfiles/functional-programming/_twice_exr.qmd b/chapters/_subfiles/functional-programming/_twice_exr.qmd index d96e7abfc..022756603 100644 --- a/chapters/_subfiles/functional-programming/_twice_exr.qmd +++ b/chapters/_subfiles/functional-programming/_twice_exr.qmd @@ -3,7 +3,8 @@ The function `twice` takes a function `f` and a value `x`: -```racket +```{racket} +#| code-fold: false (define (twice f x) (f (f x))) diff --git a/chapters/_subfiles/functional-programming/_twice_sol.qmd b/chapters/_subfiles/functional-programming/_twice_sol.qmd index 835d55cda..1af4d63ab 100644 --- a/chapters/_subfiles/functional-programming/_twice_sol.qmd +++ b/chapters/_subfiles/functional-programming/_twice_sol.qmd @@ -8,4 +8,13 @@ Replace `f` and `x` in the body `(f (f x))` (@def-racket-define): 3. The argument `(twice add-three 0)` returns 6, so the whole expression is `(twice add-three 6)`, which returns 12. +Racket returns the same values: + +```{racket} +#| code-fold: false +(twice add-three 10) +(twice h 4) +(twice add-three (twice add-three 0)) +``` + ::: diff --git a/chapters/functional-programming.qmd b/chapters/functional-programming.qmd index 24eeb4b4c..ad5308969 100644 --- a/chapters/functional-programming.qmd +++ b/chapters/functional-programming.qmd @@ -13,6 +13,8 @@ format: {{< include ../latex-macros/macros.qmd >}} +{{< include _subfiles/_racket-setup.qmd >}} + ::: {.callout-note appearance='simple' collapse=true} ## Source From 094f73fd19193d9888fc3f1f48e779bc1614b7ef Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 09:09:51 +0000 Subject: [PATCH 2/2] Typeset both arguments of the absolute-value example as math "Applied to $-7$ and to 2" set one number in math and the other in text, in the same phrase. Raised by the cross-harness read of #140. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FWgYrEUFVHj2qKjKncHg77 --- .../_subfiles/functional-programming/_absolute-value_exm.qmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapters/_subfiles/functional-programming/_absolute-value_exm.qmd b/chapters/_subfiles/functional-programming/_absolute-value_exm.qmd index 65b73beb4..04d4eedd3 100644 --- a/chapters/_subfiles/functional-programming/_absolute-value_exm.qmd +++ b/chapters/_subfiles/functional-programming/_absolute-value_exm.qmd @@ -19,7 +19,7 @@ has two cases, so one `if` (@def-if-cond) is enough: x)) ``` -Applied to $-7$ and to 2, it returns their absolute values: +Applied to $-7$ and to $2$, it returns their absolute values: ```{racket} #| code-fold: false