Resolve the issue that caused the amb language parser to fail and figure out how to get multiple amb-solutions

This commit is contained in:
2021-02-17 19:37:53 -05:00
parent 2afc4f4510
commit 80adad6179
3 changed files with 56 additions and 29 deletions

View File

@@ -100,15 +100,37 @@
(my-assert (multiple-dwelling)
'((baker 3) (cooper 2) (fletcher 4) (miller 5) (smith 1)))
(define (multiple-dwelling-removed)
(let ((baker (amb 1 2 3 4 5))
(cooper (amb 1 2 3 4 5))
(fletcher (amb 1 2 3 4 5))
(miller (amb 1 2 3 4 5))
(smith (amb 1 2 3 4 5)))
(require (distinct? (list baker cooper fletcher miller smith)))
(require (not (= baker 5)))
(require (not (= cooper 1)))
(require (not (= fletcher 5)))
(require (not (= fletcher 1)))
(require (> miller cooper))
;; (require (not (= (abs (- smith fletcher)) 1))) ; adjacent floors constraint
(require (not (= (abs (- fletcher cooper)) 1)))
(list (list 'baker baker)
(list 'cooper cooper)
(list 'fletcher fletcher)
(list 'miller miller)
(list 'smith smith))))
; There are five solutions when the adjacent floor constraint for smith and
; fletcher is removed.
(my-assert (length (set-of (multiple-dwelling-removed))) 5)
(display "\nex-4.39 - multiple-dwelling-ordering\n")
; The ordering does not matter because the interpreter first evaluates all ambs
; and then runs the checks. The interpreter will check all combinations even if
; they cannot yield a possible solution, such as (fletcher 1). To avoid this one
; would have to interleave the am expression and the checks.
; would have to interleave the amb expression and the checks.
(display "[answered]\n")
@@ -216,6 +238,20 @@
(display "\nex-4.42 - liars-puzzle\n")
; Five schoolgirls sat for an examination. Their parents -- so they thought
; -- showed an undue degree of interest in the result. They therefore agreed
; that, in writing home about the examination, each girl should make one
; true statement and one untrue one. The following are the relevant passages
; from their letters:
;
; Betty: ``Kitty was second in the examination. I was only third.''
; Ethel: ``You'll be glad to hear that I was on top. Joan was second.''
; Joan: ``I was third, and poor old Ethel was bottom.''
; Kitty: ``I came out second. Mary was only fourth.''
; Mary: ``I was fourth. Top place was taken by Betty.''
;
; What in fact was the order in which the five girls were placed?
(define (no-violation new-stmt stmts)
(if (null? stmts)
#t
@@ -330,4 +366,5 @@
(queens-iter '() 8))
(my-assert (queens) '(4 2 7 3 6 8 5 1))
(my-assert (length (set-of (queens))) 92)