Compare commits

...

3 Commits

Author SHA1 Message Date
Oliver Payne dfd65eed08 Add exercise 4.47 2024-01-04 22:59:53 +00:00
Oliver Payne 2caab6255f Add exercise 4.46 2024-01-04 22:56:06 +00:00
Oliver Payne a3b0a23812 Reformat output 2024-01-04 22:55:35 +00:00
1 changed files with 50 additions and 11 deletions

View File

@ -139,15 +139,54 @@
(sentence (sentence
(simple-noun-phrase (article the) (noun professor)) (simple-noun-phrase (article the) (noun professor))
(verb-phrase (verb lectures) (verb-phrase
(prep-phrase (prep to) (verb lectures)
(noun-phrase (simple-noun-phrase (article the) (noun student)) (prep-phrase (prep to)
(prep-phrase (prep in) (noun-phrase (simple-noun-phrase (article the) (noun student))
(noun-phrase (prep-phrase (prep in)
(simple-noun-phrase (article the) (noun-phrase
(noun class)) (simple-noun-phrase (article the)
(prep-phrase (prep with) (noun class))
(simple-noun-phrase (prep-phrase (prep with)
(article the) (simple-noun-phrase
(noun cat))))))))) (article the)
(noun cat)))))))))
;; The professor lectures to the student, who is in the class with the cat ;; The professor lectures to the student, who is in the class with the cat
;; Exercise 4.46
;; If amb evaluated its arguments in any order other than
;; left-to-right, then the recursive maybe-extend rules would never
;; terminate, as the second argument is a recursive call to the
;; procedure. We rely on the recursion stopping at each decision
;; point.
;;
;; Also, since parse has side-effects (namely, it removes a word from
;; the input), it is important that we parse the different parts of
;; the sentence in the correct order.
;; Exercise 4.47
(define (parse-verb-phrase level)
(log (list 'parse-verb-phrase level))
(amb (parse-word verbs)
(list 'verb-phrase
(parse-verb-phrase (+ level 1))
(parse-prepositional-phrase (+ level 1)))))
;; In this case, if (parse-word verbs) fails, then we evaluate the 2nd
;; argument, which calls parse-verb-phrase recursively, creating a new
;; amb, which tries (parse-word verbs), which still fails. In the
;; orinal procedure, we evaluate (parse-word verbs) once and bind it
;; to verb-phrase. We then keep extending the phrase until it either
;; succeeds or fails. If it fails, the whole amb call fails. The
;; version here never fails because it keeps calling
;; parse-verb-phrase, leading to an infinite recursion. The only case
;; where this procedure works is when we pass it a valid verb phrase
;; and don't try again (ie trigger a fail).
;; If we swap the order of the clauses in the amb, then we get an
;; infinite loop, as the first clause immediately calls
;; parse-verb-phrase recursively. There is nothing to limit this
;; recursion.