Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions chapters/_subfiles/recursion/_append_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
::: {#exm-append}

Check warning on line 1 in chapters/_subfiles/recursion/_append_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-append has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### Appending `'(1 2)` and `'(3)`

In Racket, the definition of append (@def-append) is

```racket
```{racket}
#| code-fold: false
(define (app L M)
(if (empty? L)
M
Expand All @@ -20,6 +21,12 @@
= '(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))
```

:::
3 changes: 2 additions & 1 deletion chapters/_subfiles/recursion/_binary-trace_exr.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions chapters/_subfiles/recursion/_binary-trace_sol.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

:::
7 changes: 7 additions & 0 deletions chapters/_subfiles/recursion/_cons-list_sol.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
```

:::
11 changes: 8 additions & 3 deletions chapters/_subfiles/recursion/_digits-racket_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
::: {#exm-digits-racket}

Check warning on line 1 in chapters/_subfiles/recursion/_digits-racket_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-digits-racket has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### Counting digits in Racket

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
Expand All @@ -16,7 +17,11 @@
- 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)
```

:::
22 changes: 17 additions & 5 deletions chapters/_subfiles/recursion/_first-rest_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
::: {#exm-first-rest}

Check warning on line 1 in chapters/_subfiles/recursion/_first-rest_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-first-rest has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### 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)
```

:::
11 changes: 9 additions & 2 deletions chapters/_subfiles/recursion/_list-length_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
::: {#exm-list-length}

Check warning on line 1 in chapters/_subfiles/recursion/_list-length_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-list-length has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### The length of `'(7 2 9)`

In Racket, the definition of length (@def-list-length) is

```racket
```{racket}
#| code-fold: false
(define (len L)
(if (empty? L)
0
Expand All @@ -22,6 +23,12 @@
= 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))
```

:::
2 changes: 2 additions & 0 deletions chapters/recursion.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ format:

{{< include ../latex-macros/macros.qmd >}}

{{< include _subfiles/_racket-setup.qmd >}}

::: {.callout-note appearance='simple' collapse=true}
## Source

Expand Down
Loading