diff --git a/chapters/_subfiles/recursion/_append_exm.qmd b/chapters/_subfiles/recursion/_append_exm.qmd index e9a5989e8..7d89fd7e0 100644 --- a/chapters/_subfiles/recursion/_append_exm.qmd +++ b/chapters/_subfiles/recursion/_append_exm.qmd @@ -3,7 +3,8 @@ In Racket, the definition of append (@def-append) is -```racket +```{racket} +#| code-fold: false (define (app L M) (if (empty? L) M @@ -20,6 +21,12 @@ The recursion takes `L` apart and leaves `M` alone: = '(1 2 3) ``` -Racket's built-in `append` gives the same values. +Racket's built-in `append` gives the same values: + +```{racket} +#| code-fold: false +(app '(1 2) '(3)) +(append '(1 2) '(3)) +``` ::: diff --git a/chapters/_subfiles/recursion/_binary-trace_exr.qmd b/chapters/_subfiles/recursion/_binary-trace_exr.qmd index 235b7dc78..6eeee84c7 100644 --- a/chapters/_subfiles/recursion/_binary-trace_exr.qmd +++ b/chapters/_subfiles/recursion/_binary-trace_exr.qmd @@ -4,7 +4,8 @@ The Racket function `binary` takes a non-negative integer (@def-nonnegative-integers) `n` and returns a string: -```racket +```{racket} +#| code-fold: false (define (binary n) (if (< n 2) (number->string n) diff --git a/chapters/_subfiles/recursion/_binary-trace_sol.qmd b/chapters/_subfiles/recursion/_binary-trace_sol.qmd index 3ad0b3e80..41e195fbd 100644 --- a/chapters/_subfiles/recursion/_binary-trace_sol.qmd +++ b/chapters/_subfiles/recursion/_binary-trace_sol.qmd @@ -17,4 +17,13 @@ Replacing each call with what it returns: So `(binary 1)` returns `"1"`, `(binary 3)` returns `"11"`, and `(binary 6)` returns `"110"`. That is 6 written in base 2, since $1 \cdot 4 + 1 \cdot 2 + 0 \cdot 1 = 6$. +Racket returns the same values: + +```{racket} +#| code-fold: false +(binary 1) +(binary 3) +(binary 6) +``` + ::: diff --git a/chapters/_subfiles/recursion/_cons-list_sol.qmd b/chapters/_subfiles/recursion/_cons-list_sol.qmd index 3f04570a0..191c44267 100644 --- a/chapters/_subfiles/recursion/_cons-list_sol.qmd +++ b/chapters/_subfiles/recursion/_cons-list_sol.qmd @@ -8,4 +8,11 @@ Build the list from its last element forward: So `'(7 2 9)` is `(cons 7 (cons 2 (cons 9 empty)))`. +Racket prints it the same way: + +```{racket} +#| code-fold: false +(cons 7 (cons 2 (cons 9 empty))) +``` + ::: diff --git a/chapters/_subfiles/recursion/_digits-racket_exm.qmd b/chapters/_subfiles/recursion/_digits-racket_exm.qmd index 6aa04cffe..52a0951d7 100644 --- a/chapters/_subfiles/recursion/_digits-racket_exm.qmd +++ b/chapters/_subfiles/recursion/_digits-racket_exm.qmd @@ -3,7 +3,8 @@ The recursive definition of $d$ in @exm-digit-count becomes a Racket function line by line: -```racket +```{racket} +#| code-fold: false (define (digits n) (if (< n 10) 1 @@ -16,7 +17,11 @@ Following @rem-racket-recursion: - the recursive call is `(digits (quotient n 10))`, on $\floor{n / 10}$, which is less than $n$; - the combination adds 1 to the result of the recursive call. -`(digits 2026)` calls `(digits 202)`, `(digits 20)` and `(digits 2)` in turn, as in @exm-digit-count, -and returns 4. +`(digits 2026)` calls `(digits 202)`, `(digits 20)` and `(digits 2)` in turn, as in @exm-digit-count: + +```{racket} +#| code-fold: false +(digits 2026) +``` ::: diff --git a/chapters/_subfiles/recursion/_first-rest_exm.qmd b/chapters/_subfiles/recursion/_first-rest_exm.qmd index eb29318a7..19bde83e7 100644 --- a/chapters/_subfiles/recursion/_first-rest_exm.qmd +++ b/chapters/_subfiles/recursion/_first-rest_exm.qmd @@ -2,13 +2,25 @@ #### Taking a list apart Let `L` be the list `'(7 2 9)` (@def-list), which @sol-cons-list builds as `(cons 7 (cons 2 (cons 9 empty)))`. -Then +Then `first` and `rest` take it apart: -- `(first L)` is `7`, and `(rest L)` is `'(2 9)`; -- `(first (rest L))` is `2`; -- `(rest (rest (rest L)))` is `empty`. +```{racket} +#| code-fold: false +(define L (cons 7 (cons 2 (cons 9 empty)))) +(first L) +(rest L) +(first (rest L)) +(rest (rest (rest L))) +``` +The last value, `'()`, is how Racket prints `empty`. `(first empty)` raises an error, -since the empty list is not built by `cons` and has no first element. +since the empty list is not built by `cons` and has no first element: + +```{racket} +#| code-fold: false +#| error: true +(first empty) +``` ::: diff --git a/chapters/_subfiles/recursion/_list-length_exm.qmd b/chapters/_subfiles/recursion/_list-length_exm.qmd index 773a3cde9..d75c8fa90 100644 --- a/chapters/_subfiles/recursion/_list-length_exm.qmd +++ b/chapters/_subfiles/recursion/_list-length_exm.qmd @@ -3,7 +3,8 @@ In Racket, the definition of length (@def-list-length) is -```racket +```{racket} +#| code-fold: false (define (len L) (if (empty? L) 0 @@ -22,6 +23,12 @@ Replacing each call with the recursive rule: = 3 ``` -Racket's built-in `length` gives the same values. +Racket's built-in `length` gives the same values: + +```{racket} +#| code-fold: false +(len '(7 2 9)) +(length '(7 2 9)) +``` ::: diff --git a/chapters/recursion.qmd b/chapters/recursion.qmd index 06ed1c39e..e856de372 100644 --- a/chapters/recursion.qmd +++ b/chapters/recursion.qmd @@ -13,6 +13,8 @@ format: {{< include ../latex-macros/macros.qmd >}} +{{< include _subfiles/_racket-setup.qmd >}} + ::: {.callout-note appearance='simple' collapse=true} ## Source