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
15 changes: 12 additions & 3 deletions chapters/_subfiles/relations/_racket-function_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
::: {#exm-racket-function}

Check warning on line 1 in chapters/_subfiles/relations/_racket-function_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

Example #exm-racket-function has no figure. Fix: add a figure of the example's data, model or results, or add <!-- no-figure: reason --> inside the div
#### Checking for one output per input in Racket

`(for/sum ([x L]) body)` evaluates `body` once for each element `x` of the list `L`,
Expand All @@ -7,7 +7,8 @@
`count-outputs` uses it to count the pairs whose first entry is `a`,
and `function?` uses `for/and` (@exm-racket-properties) to check the condition of @thm-function-relation for each element of the list `A`:

```racket
```{racket}
#| code-fold: false
(define (count-outputs R a)
(for/sum ([p R])
(if (equal? (first p) a) 1 0)))
Expand All @@ -17,7 +18,15 @@
(= (count-outputs R a) 1)))
```

`(function? (list 'rock 'paper 'scissors) beats)` returns `#t`, as @exm-function-or-not found,
since `(count-outputs beats a)` is 1 for each of the three elements.
`(count-outputs beats a)` is 1 for each of the three elements,
so `function?` returns `#t`, as @exm-function-or-not found:

```{racket}
#| code-fold: false
(count-outputs beats 'rock)
(count-outputs beats 'paper)
(count-outputs beats 'scissors)
(function? (list 'rock 'paper 'scissors) beats)
```

:::
3 changes: 2 additions & 1 deletion chapters/_subfiles/relations/_racket-member_exr.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

In Racket, the relation $B$ of @exm-beats can be stored as a list of two-element lists:

```racket
```{racket}
#| code-fold: false
(define beats
(list (list 'rock 'scissors)
(list 'scissors 'paper)
Expand Down
10 changes: 8 additions & 2 deletions chapters/_subfiles/relations/_racket-member_sol.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ Yes.
The list `(list 'rock 'paper)` is not an element of `beats`,
since rock does not beat paper (@sol-beats-transitive),
so `member` returns `#f`.
When `x` is an element of `L`, `member` returns the part of `L` that starts at `x`, which is not `#f`:
`(member (list 'paper 'rock) beats)` returns `'((paper rock))`.
When `x` is an element of `L`, `member` returns the part of `L` that starts at `x`, which is not `#f`,
as the second of these two calls shows:

```{racket}
#| code-fold: false
(member (list 'rock 'paper) beats)
(member (list 'paper 'rock) beats)
```

:::
17 changes: 13 additions & 4 deletions chapters/_subfiles/relations/_racket-properties_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
::: {#exm-racket-properties}

Check warning on line 1 in chapters/_subfiles/relations/_racket-properties_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

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

The predicate `related?` turns the answer of `member` into `#t` or `#f`:

```racket
```{racket}
#| code-fold: false
(define (related? R a b)
(if (member (list a b) R) #t #f))
```
Expand All @@ -14,7 +15,8 @@
does the same for every combination of values of its variables,
here every two pairs of a relation:

```racket
```{racket}
#| code-fold: false
(define (reflexive? S R)
(for/and ([a S])
(related? R a a)))
Expand All @@ -37,7 +39,14 @@
"if $b = b'$, then $a \mathrel{R} c$" (@def-transitive),
written as an `or` by @cor-conditional-as-or.

All three of `(reflexive? (list 'rock 'paper 'scissors) beats)`, `(symmetric? beats)` and `(transitive? beats)` return `#f`,
as @exm-reflexive-or-not, @exm-symmetric-or-not and @exm-transitive-or-not found.
All three return `#f` for `beats`,
as @exm-reflexive-or-not, @exm-symmetric-or-not and @exm-transitive-or-not found:

```{racket}
#| code-fold: false
(reflexive? (list 'rock 'paper 'scissors) beats)
(symmetric? beats)
(transitive? beats)
```

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