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
(simple-noun-phrase (article the) (noun professor))
(verb-phrase (verb lectures)
(prep-phrase (prep to)
(noun-phrase (simple-noun-phrase (article the) (noun student))
(prep-phrase (prep in)
(noun-phrase
(simple-noun-phrase (article the)
(noun class))
(prep-phrase (prep with)
(simple-noun-phrase
(article the)
(noun cat)))))))))
(verb-phrase
(verb lectures)
(prep-phrase (prep to)
(noun-phrase (simple-noun-phrase (article the) (noun student))
(prep-phrase (prep in)
(noun-phrase
(simple-noun-phrase (article the)
(noun class))
(prep-phrase (prep with)
(simple-noun-phrase
(article the)
(noun 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.