diff --git a/chapters/_subfiles/relations/_racket-function_exm.qmd b/chapters/_subfiles/relations/_racket-function_exm.qmd index 9bc67eb13..0a3a806d0 100644 --- a/chapters/_subfiles/relations/_racket-function_exm.qmd +++ b/chapters/_subfiles/relations/_racket-function_exm.qmd @@ -7,7 +7,8 @@ With a finite relation stored as a list of pairs, as in @exr-racket-member, `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))) @@ -17,7 +18,15 @@ and `function?` uses `for/and` (@exm-racket-properties) to check the condition o (= (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) +``` ::: diff --git a/chapters/_subfiles/relations/_racket-member_exr.qmd b/chapters/_subfiles/relations/_racket-member_exr.qmd index 11e0c868a..04ac21c80 100644 --- a/chapters/_subfiles/relations/_racket-member_exr.qmd +++ b/chapters/_subfiles/relations/_racket-member_exr.qmd @@ -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) diff --git a/chapters/_subfiles/relations/_racket-member_sol.qmd b/chapters/_subfiles/relations/_racket-member_sol.qmd index 98162aae2..241aefe6e 100644 --- a/chapters/_subfiles/relations/_racket-member_sol.qmd +++ b/chapters/_subfiles/relations/_racket-member_sol.qmd @@ -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) +``` ::: diff --git a/chapters/_subfiles/relations/_racket-properties_exm.qmd b/chapters/_subfiles/relations/_racket-properties_exm.qmd index 81d75f860..4ed80aec2 100644 --- a/chapters/_subfiles/relations/_racket-properties_exm.qmd +++ b/chapters/_subfiles/relations/_racket-properties_exm.qmd @@ -3,7 +3,8 @@ 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)) ``` @@ -14,7 +15,8 @@ and returns `#f` as soon as `body` returns `#f`, and `#t` if it never does. 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))) @@ -37,7 +39,14 @@ here every two pairs of a relation: "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) +``` ::: diff --git a/chapters/relations.qmd b/chapters/relations.qmd index 4bbd2ac64..6688dcd93 100644 --- a/chapters/relations.qmd +++ b/chapters/relations.qmd @@ -13,6 +13,8 @@ format: {{< include ../latex-macros/macros.qmd >}} +{{< include _subfiles/_racket-setup.qmd >}} + ::: {.callout-note appearance='simple' collapse=true} ## Source