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
12 changes: 10 additions & 2 deletions chapters/_subfiles/functional-programming/_absolute-value_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
::: {#exm-absolute-value}

Check warning on line 1 in chapters/_subfiles/functional-programming/_absolute-value_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-absolute-value has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### Absolute value with `if`

The absolute value
Expand All @@ -11,14 +11,22 @@
$$
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`.

:::
10 changes: 10 additions & 0 deletions chapters/_subfiles/functional-programming/_evaluate-nested_sol.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

:::
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

This Racket code defines a function named `h`:

```racket
```{racket}
#| code-fold: false
(define (h x)
(- (* x x) (* 2 x)))
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
```

:::
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions chapters/_subfiles/functional-programming/_ticket-price_sol.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

:::
3 changes: 2 additions & 1 deletion chapters/_subfiles/functional-programming/_twice_exr.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down
9 changes: 9 additions & 0 deletions chapters/_subfiles/functional-programming/_twice_sol.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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))
```

:::
2 changes: 2 additions & 0 deletions chapters/functional-programming.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