From ec24d9424faa575544c010c2454c09996801e6be Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 10:16:59 +0000 Subject: [PATCH 1/2] Run the graphs chapter's Racket examples at render time office, neighbors and degree, bfs and dfs were plain fences, and the lists they return were typed in the prose (#131). They are now {racket} chunks, the typed values are chunk output, and the solution to exr-racket-assoc ends with a chunk that runs the lookup. Part of #131. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FWgYrEUFVHj2qKjKncHg77 --- .../_subfiles/graphs/_racket-assoc_exr.qmd | 3 ++- .../_subfiles/graphs/_racket-assoc_sol.qmd | 8 ++++++++ chapters/_subfiles/graphs/_racket-bfs_exm.qmd | 11 ++++++++--- chapters/_subfiles/graphs/_racket-dfs_exm.qmd | 16 ++++++++++++---- .../graphs/_racket-neighbors_exm.qmd | 19 +++++++++++++------ chapters/graphs.qmd | 2 ++ 6 files changed, 45 insertions(+), 14 deletions(-) diff --git a/chapters/_subfiles/graphs/_racket-assoc_exr.qmd b/chapters/_subfiles/graphs/_racket-assoc_exr.qmd index bddf36b41..9a982e9fc 100644 --- a/chapters/_subfiles/graphs/_racket-assoc_exr.qmd +++ b/chapters/_subfiles/graphs/_racket-assoc_exr.qmd @@ -4,7 +4,8 @@ In Racket, the adjacency list of the office graph (@exm-adjacency-list) can be stored as a list with one list for each vertex, which starts with the vertex and goes on with its neighbors: -```racket +```{racket} +#| code-fold: false (define office '((a b c) (b a c d) diff --git a/chapters/_subfiles/graphs/_racket-assoc_sol.qmd b/chapters/_subfiles/graphs/_racket-assoc_sol.qmd index f52ad72e3..8abe1bbdd 100644 --- a/chapters/_subfiles/graphs/_racket-assoc_sol.qmd +++ b/chapters/_subfiles/graphs/_racket-assoc_sol.qmd @@ -4,4 +4,12 @@ It returns `'(d b e f)`, the element of `office` that starts with `d`. Its rest, `'(b e f)`, lists the neighbors of $d$ (@def-adjacent). +Racket returns the same values: + +```{racket} +#| code-fold: false +(assoc 'd office) +(rest (assoc 'd office)) +``` + ::: diff --git a/chapters/_subfiles/graphs/_racket-bfs_exm.qmd b/chapters/_subfiles/graphs/_racket-bfs_exm.qmd index d670d73ee..9226456a2 100644 --- a/chapters/_subfiles/graphs/_racket-bfs_exm.qmd +++ b/chapters/_subfiles/graphs/_racket-bfs_exm.qmd @@ -6,7 +6,8 @@ one for each marked vertex $u$ with label $d$, so a vertex is marked when `assoc` (@exr-racket-assoc) finds it in that list. It uses `neighbors` from @exm-racket-neighbors. -```racket +```{racket} +#| code-fold: false (define (unmarked vs dist) (if (empty? vs) empty @@ -40,7 +41,11 @@ It uses `neighbors` from @exm-racket-neighbors. so `(append (rest queue) new)` removes the front of the queue and adds the new vertices at the back. Each call of `bfs-loop` is one pass through the loop of @def-bfs. -`(bfs office 'a)` returns `'((a 0) (b 1) (c 1) (d 2) (e 2) (f 3))`, -the marks and labels of @exm-bfs-trace. +From $a$, it returns the marks and labels of @exm-bfs-trace: + +```{racket} +#| code-fold: false +(bfs office 'a) +``` ::: diff --git a/chapters/_subfiles/graphs/_racket-dfs_exm.qmd b/chapters/_subfiles/graphs/_racket-dfs_exm.qmd index 0fa09fc3e..87dd5d319 100644 --- a/chapters/_subfiles/graphs/_racket-dfs_exm.qmd +++ b/chapters/_subfiles/graphs/_racket-dfs_exm.qmd @@ -8,7 +8,8 @@ and `dfs-each` goes through a list `vs` of vertices, calling `dfs-from` on each one that is not marked yet. It uses `neighbors` from @exm-racket-neighbors. -```racket +```{racket} +#| code-fold: false (define (dfs-from G v marked) (dfs-each G (neighbors G v) (append marked (list v)))) @@ -25,7 +26,8 @@ It uses `neighbors` from @exm-racket-neighbors. With the graph $T$ of @fig-triangles stored as -```racket +```{racket} +#| code-fold: false (define triangles '((1 2 3) (2 1 3) @@ -36,7 +38,13 @@ With the graph $T$ of @fig-triangles stored as (7 5 6))) ``` -`(dfs triangles 3)` returns `'(3 1 2 4 5 6 7)`, the order of @exm-dfs-trace, -and `(dfs office 'a)` returns `'(a b c e d f)`, the order of @sol-follow-first. +`(dfs triangles 3)` returns the order of @exm-dfs-trace, +and `(dfs office 'a)` returns the order of @sol-follow-first: + +```{racket} +#| code-fold: false +(dfs triangles 3) +(dfs office 'a) +``` ::: diff --git a/chapters/_subfiles/graphs/_racket-neighbors_exm.qmd b/chapters/_subfiles/graphs/_racket-neighbors_exm.qmd index 6665c8713..b46a3db2a 100644 --- a/chapters/_subfiles/graphs/_racket-neighbors_exm.qmd +++ b/chapters/_subfiles/graphs/_racket-neighbors_exm.qmd @@ -4,7 +4,8 @@ With `office` as in @exr-racket-assoc, these functions return the neighbors (@def-adjacent) and the degree (@def-degree) of a vertex of a graph with no loops or parallel edges: -```racket +```{racket} +#| code-fold: false (define (neighbors G v) (rest (assoc v G))) @@ -12,15 +13,21 @@ these functions return the neighbors (@def-adjacent) and the degree (@def-degree (length (neighbors G v))) ``` -`(neighbors office 'b)` returns `'(a c d)`, and `(degree office 'b)` returns 3. +For the vertex $b$: + +```{racket} +#| code-fold: false +(neighbors office 'b) +(degree office 'b) +``` + `(for/sum ([x L]) body)` adds up the values of `body` for each element `x` of the list `L`, -so +so this sum of the degrees of the six vertices is $2 \card{E}$, as @thm-handshake says: -```racket +```{racket} +#| code-fold: false (for/sum ([row office]) (degree office (first row))) ``` -adds up the degrees of the six vertices and returns 16, which is $2 \card{E}$, as @thm-handshake says. - ::: diff --git a/chapters/graphs.qmd b/chapters/graphs.qmd index 37c2c5c2c..ae3b90149 100644 --- a/chapters/graphs.qmd +++ b/chapters/graphs.qmd @@ -13,6 +13,8 @@ format: {{< include ../latex-macros/macros.qmd >}} +{{< include _subfiles/_racket-setup.qmd >}} + ::: {.callout-note appearance='simple' collapse=true} ## Source From b8f4a11a41ee676544cbcaafef873ad1e7c1ca3c Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 09:16:22 +0000 Subject: [PATCH 2/2] Give the handshake theorem as the reason, and name bfs The degree sum is 2|E| because of the handshake theorem, not because of how for/sum works, so split the sentence: for/sum explains what the chunk adds, and the theorem gives the value. In the breadth-first search example, "it returns" had "each call of bfs-loop" as its nearest antecedent; name bfs. Raised by the cross-harness read of #144. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FWgYrEUFVHj2qKjKncHg77 --- chapters/_subfiles/graphs/_racket-bfs_exm.qmd | 2 +- chapters/_subfiles/graphs/_racket-neighbors_exm.qmd | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/chapters/_subfiles/graphs/_racket-bfs_exm.qmd b/chapters/_subfiles/graphs/_racket-bfs_exm.qmd index 9226456a2..79fab22c1 100644 --- a/chapters/_subfiles/graphs/_racket-bfs_exm.qmd +++ b/chapters/_subfiles/graphs/_racket-bfs_exm.qmd @@ -41,7 +41,7 @@ It uses `neighbors` from @exm-racket-neighbors. so `(append (rest queue) new)` removes the front of the queue and adds the new vertices at the back. Each call of `bfs-loop` is one pass through the loop of @def-bfs. -From $a$, it returns the marks and labels of @exm-bfs-trace: +From $a$, `bfs` returns the marks and labels of @exm-bfs-trace: ```{racket} #| code-fold: false diff --git a/chapters/_subfiles/graphs/_racket-neighbors_exm.qmd b/chapters/_subfiles/graphs/_racket-neighbors_exm.qmd index b46a3db2a..6453276ff 100644 --- a/chapters/_subfiles/graphs/_racket-neighbors_exm.qmd +++ b/chapters/_subfiles/graphs/_racket-neighbors_exm.qmd @@ -22,7 +22,8 @@ For the vertex $b$: ``` `(for/sum ([x L]) body)` adds up the values of `body` for each element `x` of the list `L`, -so this sum of the degrees of the six vertices is $2 \card{E}$, as @thm-handshake says: +so this chunk adds up the degrees of the six vertices. +By @thm-handshake, that sum is $2 \card{E}$: ```{racket} #| code-fold: false