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: 7 additions & 5 deletions chapters/_subfiles/automata/_racket-numeral_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
::: {#exm-racket-numeral}

Check warning on line 1 in chapters/_subfiles/automata/_racket-numeral_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

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

In the form of @exr-racket-step, the DFA of @sol-decimal-numeral is the table below,
with the symbol `'digit` for the input $\mathtt{d}$ and the symbol `'point` for the input $\mathtt{.}$:

```racket
```{racket}
#| code-fold: false
(define numeral-table
'((n0 (digit n1) (point n4))
(n1 (digit n1) (point n2))
Expand All @@ -16,7 +17,8 @@
To check real text, `classify` turns each character into an input,
and `numeral?` runs the DFA with `accepts?` from @exm-racket-run:

```racket
```{racket}
#| code-fold: false
(define (classify c)
(cond [(char<=? #\0 c #\9) 'digit]
[(char=? c #\.) 'point]
Expand All @@ -35,13 +37,13 @@

A character that is neither a digit nor a point is classified `'other`, which is not an input of the DFA,
so `numeral?` returns `#f` for any text that contains one, without running the DFA.
Then
Applied to eight strings, `numeral?` returns:

```racket
```{racket}
#| code-fold: false
(map numeral? '("3.14" "2026" "7." ".5" "1.2.3" "d.d" "-3" "\u00BD"))
```

returns `'(#t #t #f #f #f #f #f #f)`:
`"3.14"` and `"2026"` are numerals, and the others are not.
The letter d in `"d.d"` is classified `'other`, not `'digit`,
and so is the one character of `"\u00BD"`, the fraction one half,
Expand Down
25 changes: 19 additions & 6 deletions chapters/_subfiles/automata/_racket-run_exm.qmd
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
::: {#exm-racket-run}

Check warning on line 1 in chapters/_subfiles/automata/_racket-run_exm.qmd

View workflow job for this annotation

GitHub Actions / check / check-exm-figures

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

With `step` as in @exr-racket-step,
`run` returns the run (@def-run) from a state `s` on a list `xs` of inputs,
and `accepts?` says whether a DFA accepts (@def-accept) the string whose inputs are the list `xs`:

```racket
```{racket}
#| code-fold: false
(define (run table s xs)
(if (empty? xs)
(list s)
Expand All @@ -21,11 +22,23 @@
- `(member x L)` is `#f` if `x` is not in the list `L`, and a list that is not `#f` if it is.
- `(string->list w)` is the list of the characters of the string `w`.

`(run pairs-table 'q0 (string->list "abab"))` returns `'(q0 q1 q0 q1 q0)`,
and `(run pairs-table 'q0 (string->list "aab"))` returns `'(q0 q1 q2 q2)`, the runs of @exm-pairs-accept.
`(accepts? pairs-table 'q0 '(q0) (string->list "abab"))` returns `#t`,
`(accepts? pairs-table 'q0 '(q0) (string->list "ba"))` returns `#f`,
and `(accepts? pairs-table 'q0 '(q0) (string->list ""))` returns `#t`, since $A$ accepts $\lambda$.
`run` returns the runs of @exm-pairs-accept on `"abab"` and `"aab"`:

```{racket}
#| code-fold: false
(run pairs-table 'q0 (string->list "abab"))
(run pairs-table 'q0 (string->list "aab"))
```

$A$ accepts `"abab"` and the empty string $\lambda$, but not `"ba"`:

```{racket}
#| code-fold: false
(accepts? pairs-table 'q0 '(q0) (string->list "abab"))
(accepts? pairs-table 'q0 '(q0) (string->list ""))
(accepts? pairs-table 'q0 '(q0) (string->list "ba"))
```

Let $w$ be the string whose symbols are the elements of `xs`, in order.
`run` reads the first symbol of $w$ first, as @def-run does, so by @thm-run-last the last state of `(run table s xs)` is $\delta^*(s, w)$.

Expand Down
3 changes: 2 additions & 1 deletion chapters/_subfiles/automata/_racket-step_exr.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
In Racket, the table of the DFA $A$ (@exm-pairs-table) can be stored as a list with one list for each state,
which starts with the state and goes on with a list `(x t)` for each input `x`, where `t` is the state that `x` leads to:

```racket
```{racket}
#| code-fold: false
(define pairs-table
'((q0 (#\a q1) (#\b q0))
(q1 (#\a q2) (#\b q0))
Expand Down
9 changes: 9 additions & 0 deletions chapters/_subfiles/automata/_racket-step_sol.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@ and its rest is `'((#\a q2) (#\b q0))`.
`(assoc #\b ...)` finds `'(#\b q0)` in that list, and its second element is `'q0`.
So `(step pairs-table 'q1 #\b)` returns `'q0`, which is $\delta(q_1, \mathtt{b})$ (@exm-pairs-dfa).

Racket returns the same values:

```{racket}
#| code-fold: false
(assoc 'q1 pairs-table)
(rest (assoc 'q1 pairs-table))
(step pairs-table 'q1 #\b)
```

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