Chapter 4, Metalinguistic Abstraction
Exercise 4.49
The main thing to notice is we need to have some dummy input. Else, we get stuck in a loop where sentence are generated by just appending a same preposition clause!
The dummy input can be used to make sure that our sentence won’t get past this dummy input length and thus when that length reaches, amb
tries for other words and thus we get different sentences.
Here goes the code:
1
2
3
4
5
6
7
8
9
(define (list-amb l)
(require (not (null? l)))
(amb (car l) (list-amb (cdr l))))
(define (parse-word word-list)
(require (not (null? *unparsed*)))
(let ((found-word (list-amb (cdr word-list))))
(set! *unparsed* (cdr *unparsed*))
(list (car word-list) found-word)))
Output:
3 length input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
;;; Amb-Eval input:
(parse '(dummy dummy dummy))
;;; Starting a new problem
;;; Amb-Eval value:
ok
;;; Amb-Eval input:
;;; Starting a new problem
;;; Amb-Eval value:
(sentence (simple-noun-phrase (article the) (noun student)) (verb studies))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
(sentence (simple-noun-phrase (article the) (noun student)) (verb lectures))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
(sentence (simple-noun-phrase (article the) (noun student)) (verb eats))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
(sentence (simple-noun-phrase (article the) (noun student)) (verb sleeps))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
(sentence (simple-noun-phrase (article the) (noun professor)) (verb studies))
;;; Amb-Eval input:
6 length input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(parse '(dummy dummy dummy dummy dummmy dummy))
;;; Starting a new problem
;;; Amb-Eval value:
(sentence (simple-noun-phrase (article the) (noun student)) (verb-phrase (verb studies) (prep-phrase (prep for) (simple-noun-phrase (article the) (noun student)))))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
(sentence (simple-noun-phrase (article the) (noun student)) (verb-phrase (verb studies) (prep-phrase (prep for) (simple-noun-phrase (article the) (noun professor)))))
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
(sentence (simple-noun-phrase (article the) (noun student)) (verb-phrase (verb studies) (prep-phrase (prep for) (simple-noun-phrase (article the) (noun cat)))))