From 6b62d5607e6e2ec5274bdfc543888124c031d0c3 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 10:15:47 +0000 Subject: [PATCH 1/3] Run the trees chapter's Racket examples at render time The binary-tree examples were plain fences, with the traversals, sizes, heights and values typed in the prose (#131). They are now {racket} chunks, the typed values are chunk output, and the solution to exr-racket-tree ends with a chunk that walks (first (second (third B))) one step at a time, as its reasoning does. Part of #131. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FWgYrEUFVHj2qKjKncHg77 --- chapters/_subfiles/trees/_racket-evaluate_exm.qmd | 15 +++++++++++---- chapters/_subfiles/trees/_racket-height_exm.qmd | 13 ++++++++++--- .../_subfiles/trees/_racket-level-order_exm.qmd | 11 +++++++++-- chapters/_subfiles/trees/_racket-postfix_exm.qmd | 10 ++++++++-- .../_subfiles/trees/_racket-traversals_exm.qmd | 13 +++++++++---- chapters/_subfiles/trees/_racket-tree_exr.qmd | 3 ++- chapters/_subfiles/trees/_racket-tree_sol.qmd | 9 +++++++++ chapters/trees.qmd | 2 ++ 8 files changed, 60 insertions(+), 16 deletions(-) diff --git a/chapters/_subfiles/trees/_racket-evaluate_exm.qmd b/chapters/_subfiles/trees/_racket-evaluate_exm.qmd index e7013cc4b..5524fd5f4 100644 --- a/chapters/_subfiles/trees/_racket-evaluate_exm.qmd +++ b/chapters/_subfiles/trees/_racket-evaluate_exm.qmd @@ -6,7 +6,8 @@ and as a list `(op a b)` for an operation `op` with operands `a` and `b`. The tree of @fig-expression is `'(* (- 7 2) (+ 3 (* 4 2)))`. These functions compute its value, working from the leaves up: -```racket +```{racket} +#| code-fold: false (define (apply-op op a b) (cond [(eq? op '+) (+ a b)] [(eq? op '-) (- a b)] @@ -23,8 +24,14 @@ These functions compute its value, working from the leaves up: - `(eq? x y)` is true when `x` and `y` are the same symbol. - `(number? e)` is true when `e` is a number. -`(evaluate '(* (- 7 2) (+ 3 (* 4 2))))` returns 55. -Without the quote, `(* (- 7 2) (+ 3 (* 4 2)))` is itself Racket code, and it also returns 55: -Racket writes each arithmetic operation in prefix form (@def-prefix-form), with a pair of parentheses around each operation. +`evaluate` computes the value of the tree. +Without the quote, `(* (- 7 2) (+ 3 (* 4 2)))` is itself Racket code, and it has the same value, +because Racket writes each arithmetic operation in prefix form (@def-prefix-form), with a pair of parentheses around each operation: + +```{racket} +#| code-fold: false +(evaluate '(* (- 7 2) (+ 3 (* 4 2)))) +(* (- 7 2) (+ 3 (* 4 2))) +``` ::: diff --git a/chapters/_subfiles/trees/_racket-height_exm.qmd b/chapters/_subfiles/trees/_racket-height_exm.qmd index 098b32732..033e18b66 100644 --- a/chapters/_subfiles/trees/_racket-height_exm.qmd +++ b/chapters/_subfiles/trees/_racket-height_exm.qmd @@ -3,7 +3,8 @@ The recursive formulas of @rem-size-height become -```racket +```{racket} +#| code-fold: false (define (size T) (if (empty? T) 0 @@ -17,7 +18,13 @@ The recursive formulas of @rem-size-height become Here `(max a b)` returns the larger of the numbers `a` and `b`. -With `B` as in @exr-racket-tree, `(size B)` returns 9 and `(height B)` returns 3, -and `(height '())` returns -1. +With `B` as in @exr-racket-tree, and with the empty tree: + +```{racket} +#| code-fold: false +(size B) +(height B) +(height '()) +``` ::: diff --git a/chapters/_subfiles/trees/_racket-level-order_exm.qmd b/chapters/_subfiles/trees/_racket-level-order_exm.qmd index 14cf8b2d2..0dec9b3c7 100644 --- a/chapters/_subfiles/trees/_racket-level-order_exm.qmd +++ b/chapters/_subfiles/trees/_racket-level-order_exm.qmd @@ -6,7 +6,8 @@ Here the queue holds subtrees rather than vertices: removing a subtree that is not empty lists its root and adds its two subtrees at the back, and removing an empty tree adds nothing. -```racket +```{racket} +#| code-fold: false (define (level-loop queue) (if (empty? queue) empty @@ -26,7 +27,13 @@ and removing an empty tree adds nothing. - `(append xs ys)` is the list of the elements of `xs` followed by those of `ys`, so the two subtrees go to the back of the queue. -With `B` as in @exr-racket-tree, `(level-order B)` returns `'(m f t c h r w j p)`, the level order of @exm-level-order. +With `B` as in @exr-racket-tree, `level-order` returns the level order of @exm-level-order: + +```{racket} +#| code-fold: false +(level-order B) +``` + Each subtree that is not empty enters the queue when its parent's subtree is removed, just as `bfs` marks a child when it removes the parent. diff --git a/chapters/_subfiles/trees/_racket-postfix_exm.qmd b/chapters/_subfiles/trees/_racket-postfix_exm.qmd index 312e70029..96437cdb3 100644 --- a/chapters/_subfiles/trees/_racket-postfix_exm.qmd +++ b/chapters/_subfiles/trees/_racket-postfix_exm.qmd @@ -5,7 +5,8 @@ The method of @exm-postfix-stack uses a list as the stack (@def-stack): `cons` pushes, and `first` and `rest` pop. With `apply-op` from @exm-racket-evaluate, -```racket +```{racket} +#| code-fold: false (define (postfix-loop symbols stack) (cond [(empty? symbols) (first stack)] [(number? (first symbols)) @@ -22,6 +23,11 @@ With `apply-op` from @exm-racket-evaluate, ``` In the last clause, `(first stack)` is the number $b$ popped first and `(second stack)` is the number $a$ under it. -`(evaluate-postfix '(7 2 - 3 4 2 * + *))` returns 55. +The postfix form of the tree of @fig-expression has the same value as the tree: + +```{racket} +#| code-fold: false +(evaluate-postfix '(7 2 - 3 4 2 * + *)) +``` ::: diff --git a/chapters/_subfiles/trees/_racket-traversals_exm.qmd b/chapters/_subfiles/trees/_racket-traversals_exm.qmd index 30d44607b..43c8feed9 100644 --- a/chapters/_subfiles/trees/_racket-traversals_exm.qmd +++ b/chapters/_subfiles/trees/_racket-traversals_exm.qmd @@ -4,7 +4,8 @@ These functions follow the definitions of preorder (@def-preorder), inorder (@def-inorder) and postorder (@def-postorder), with `B` as in @exr-racket-tree: -```racket +```{racket} +#| code-fold: false (define (preorder T) (if (empty? T) empty @@ -26,9 +27,13 @@ with `B` as in @exr-racket-tree: ``` `(append L M N)` is the list of the elements of `L`, then `M`, then `N`. +The three traversals of `B` are: -- `(preorder B)` returns `'(m f c h j t r p w)`. -- `(inorder B)` returns `'(c f h j m p r t w)`. -- `(postorder B)` returns `'(c j h f p r w t m)`. +```{racket} +#| code-fold: false +(preorder B) +(inorder B) +(postorder B) +``` ::: diff --git a/chapters/_subfiles/trees/_racket-tree_exr.qmd b/chapters/_subfiles/trees/_racket-tree_exr.qmd index 1de2a7379..743886dd6 100644 --- a/chapters/_subfiles/trees/_racket-tree_exr.qmd +++ b/chapters/_subfiles/trees/_racket-tree_exr.qmd @@ -5,7 +5,8 @@ In Racket, store the empty tree (@def-binary-tree) as `'()`, and $\operatorname{node}(x, L, R)$ as the list `(list x L R)` of its label and its two subtrees. The binary tree $B$ of @exm-binary-tree is then -```racket +```{racket} +#| code-fold: false (define B '(m (f (c () ()) (h () (j () ()))) diff --git a/chapters/_subfiles/trees/_racket-tree_sol.qmd b/chapters/_subfiles/trees/_racket-tree_sol.qmd index 94b3fef81..36aa2f25e 100644 --- a/chapters/_subfiles/trees/_racket-tree_sol.qmd +++ b/chapters/_subfiles/trees/_racket-tree_sol.qmd @@ -5,4 +5,13 @@ It returns `'r`. Its second element is its left subtree, `'(r (p () ()) ())`, and the first element of that is its label, `'r`. +Racket returns the same values: + +```{racket} +#| code-fold: false +(third B) +(second (third B)) +(first (second (third B))) +``` + ::: diff --git a/chapters/trees.qmd b/chapters/trees.qmd index 57f0657a4..08227e2df 100644 --- a/chapters/trees.qmd +++ b/chapters/trees.qmd @@ -13,6 +13,8 @@ format: {{< include ../latex-macros/macros.qmd >}} +{{< include _subfiles/_racket-setup.qmd >}} + ::: {.callout-note appearance='simple' collapse=true} ## Source From 1905012454f37b7dfd9aaa49fa8ec05028d6c735 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 10:26:47 +0000 Subject: [PATCH 2/3] Link prefix form in exm-racket-evaluate instead of a trailing crossref check-avoided-words flags a trailing (@def-x) crossref on a changed line. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FWgYrEUFVHj2qKjKncHg77 --- chapters/_subfiles/trees/_racket-evaluate_exm.qmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapters/_subfiles/trees/_racket-evaluate_exm.qmd b/chapters/_subfiles/trees/_racket-evaluate_exm.qmd index 5524fd5f4..3661155ba 100644 --- a/chapters/_subfiles/trees/_racket-evaluate_exm.qmd +++ b/chapters/_subfiles/trees/_racket-evaluate_exm.qmd @@ -26,7 +26,7 @@ These functions compute its value, working from the leaves up: `evaluate` computes the value of the tree. Without the quote, `(* (- 7 2) (+ 3 (* 4 2)))` is itself Racket code, and it has the same value, -because Racket writes each arithmetic operation in prefix form (@def-prefix-form), with a pair of parentheses around each operation: +because Racket writes each arithmetic operation in [prefix form](#def-prefix-form), with a pair of parentheses around each operation: ```{racket} #| code-fold: false From 0e592016acc4f8d64109c4d76dd06fa239c469f7 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 09:15:27 +0000 Subject: [PATCH 3/3] Say what the height example's calls compute "With `B` as in @exr-racket-tree, and with the empty tree:" had no verb. Name what the chunk below it computes. Raised by the cross-harness read of #143. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FWgYrEUFVHj2qKjKncHg77 --- chapters/_subfiles/trees/_racket-height_exm.qmd | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/chapters/_subfiles/trees/_racket-height_exm.qmd b/chapters/_subfiles/trees/_racket-height_exm.qmd index 033e18b66..351effceb 100644 --- a/chapters/_subfiles/trees/_racket-height_exm.qmd +++ b/chapters/_subfiles/trees/_racket-height_exm.qmd @@ -18,7 +18,8 @@ The recursive formulas of @rem-size-height become Here `(max a b)` returns the larger of the numbers `a` and `b`. -With `B` as in @exr-racket-tree, and with the empty tree: +With `B` as in @exr-racket-tree, +these calls give the size and the height of $B$, and the height of the empty tree: ```{racket} #| code-fold: false