Move exercises 4.50 and onwards out of amb-natural-language.rkt

This commit is contained in:
Oliver Payne 2024-01-30 22:25:52 +00:00
parent 2d7944f89b
commit c07eef9e9e
2 changed files with 141 additions and 143 deletions

View File

@ -0,0 +1,141 @@
;; Exercise 4.50. Using ramb for generating sentences stops the
;; generation getting stuck in the out recursion.
;;; Amb-Eval input:
(set! parse-word generate-word-ramb)
;;; Starting a new problem
;;; Amb-Eval value:
0
ok
;;; Amb-Eval input:
(parse '())
;;; Starting a new problem
;;; Amb-Eval value:
0
(simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
4
(compound-sentence (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies)) (connective and) (simple-sentence (simple-noun-phrase (article a) ((noun cat))) (verb studies)))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
8
(compound-sentence (compound-sentence (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies)) (connective and) (simple-sentence (simple-noun-phrase (article a) ((noun cat))) (verb studies))) (connective and) (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies)))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
13
(compound-sentence (compound-sentence (compound-sentence (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies)) (connective and) (simple-sentence (simple-noun-phrase (article a) ((noun cat))) (verb studies))) (connective and) (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies))) (connective and) (simple-sentence (simple-noun-phrase (article the) ((noun cat))) (verb sleeps)))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
18
(compound-sentence (compound-sentence (compound-sentence (compound-sentence (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies)) (connective and) (simple-sentence (simple-noun-phrase (article a) ((noun cat))) (verb studies))) (connective and) (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies))) (connective and) (simple-sentence (simple-noun-phrase (article the) ((noun cat))) (verb sleeps))) (connective and) (simple-sentence (simple-noun-phrase (article the) ((noun student))) (verb studies)))
;;; Amb-Eval input:
;; Exercise 4.51:
;;; Amb-Eval input:
(define count 0)
(let ((x (an-element-of '(a b c)))
(y (an-element-of '(a b c))))
(permanent-set! count (+ count 1))
(require (not (eq? x y)))
(list x y count))
;;; Starting a new problem
;;; Amb-Eval value:
0
ok
;;; Amb-Eval input:
;;; Starting a new problem
;;; Amb-Eval value:
0
(a b 2)
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
3
(a c 3)
;; Using set! instead of permanent-set! resets the counter back to 0
;; on each backtrack of x or y, so will always be 1.
;; Exercise 4.52
;;; Amb-Eval input:
;;; Amb-Eval input:
(if-fail (let ((x (an-element-of '(1 3 5))))
(require (even? x))
x)
'all-odd)
;;; Starting a new problem
;;; Amb-Eval value:
0
all-odd
;;; Amb-Eval input:
(if-fail (let ((x (an-element-of '(1 3 5 8))))
(require (even? x))
x)
'all-odd)
;;; Starting a new problem
;;; Amb-Eval value:
0
8
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
4
all-odd
;;; Amb-Eval input:
try-again
;;; There are no more values of
7
(if-fail (let ((x (an-element-of (quote (1 3 5 8))))) (require (even? x)) x) (quote all-odd))

View File

@ -295,146 +295,3 @@ try-again
;; The generation gets stuck in the outer-most recursion. In this
;; case, it is the generation of compound sentences from simple sentences.
;; Exercise 4.50. Using ramb for generating sentences stops the
;; generation getting stuck in the out recursion.
;;; Amb-Eval input:
(set! parse-word generate-word-ramb)
;;; Starting a new problem
;;; Amb-Eval value:
0
ok
;;; Amb-Eval input:
(parse '())
;;; Starting a new problem
;;; Amb-Eval value:
0
(simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
4
(compound-sentence (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies)) (connective and) (simple-sentence (simple-noun-phrase (article a) ((noun cat))) (verb studies)))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
8
(compound-sentence (compound-sentence (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies)) (connective and) (simple-sentence (simple-noun-phrase (article a) ((noun cat))) (verb studies))) (connective and) (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies)))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
13
(compound-sentence (compound-sentence (compound-sentence (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies)) (connective and) (simple-sentence (simple-noun-phrase (article a) ((noun cat))) (verb studies))) (connective and) (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies))) (connective and) (simple-sentence (simple-noun-phrase (article the) ((noun cat))) (verb sleeps)))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
18
(compound-sentence (compound-sentence (compound-sentence (compound-sentence (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies)) (connective and) (simple-sentence (simple-noun-phrase (article a) ((noun cat))) (verb studies))) (connective and) (simple-sentence (simple-noun-phrase (article the) ((noun professor))) (verb studies))) (connective and) (simple-sentence (simple-noun-phrase (article the) ((noun cat))) (verb sleeps))) (connective and) (simple-sentence (simple-noun-phrase (article the) ((noun student))) (verb studies)))
;;; Amb-Eval input:
;; Exercise 4.51:
;;; Amb-Eval input:
(define count 0)
(let ((x (an-element-of '(a b c)))
(y (an-element-of '(a b c))))
(permanent-set! count (+ count 1))
(require (not (eq? x y)))
(list x y count))
;;; Starting a new problem
;;; Amb-Eval value:
0
ok
;;; Amb-Eval input:
;;; Starting a new problem
;;; Amb-Eval value:
0
(a b 2)
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
3
(a c 3)
;; Using set! instead of permanent-set! resets the counter back to 0
;; on each backtrack of x or y, so will always be 1.
;; Exercise 4.52
;;; Amb-Eval input:
;;; Amb-Eval input:
(if-fail (let ((x (an-element-of '(1 3 5))))
(require (even? x))
x)
'all-odd)
;;; Starting a new problem
;;; Amb-Eval value:
0
all-odd
;;; Amb-Eval input:
(if-fail (let ((x (an-element-of '(1 3 5 8))))
(require (even? x))
x)
'all-odd)
;;; Starting a new problem
;;; Amb-Eval value:
0
8
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
4
all-odd
;;; Amb-Eval input:
try-again
;;; There are no more values of
7
(if-fail (let ((x (an-element-of (quote (1 3 5 8))))) (require (even? x)) x) (quote all-odd))