Exercise 4.42. Solve the following ``Liars'' puzzle (from Phillips 1934): 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 (excluding x items) (cond ((null? items) items) ((= (car items) x) (excluding x (cdr items))) (else (cons (car items) (excluding x (cdr items)))))) (define (an-element-of items) ;; from text (require (not (null? items))) (amb (car items) (an-element-of (cdr items)))) (define (half-right a b) ; aka XOR (not (eq? a b))) (define (girls-results) (let ((betty (amb 1 2 3 4 5))) (let ((remaining4 (excluding betty '(1 2 3 4 5)))) (let ((ethel (an-element-of remaining4))) (let ((remaining3 (excluding ethel remaining4))) (let ((joan (an-element-of remaining3))) (let ((remaining2 (excluding joan remaining3))) (let ((kitty (an-element-of remaining2))) (let ((mary (car (excluding kitty remaining2)))) (require (and (half-right (= kitty 2) (= betty 3)) (half-right (= ethel 1) (= joan 2)) (half-right (= joan 3) (= ethel 5)) (half-right (= kitty 2) (= mary 4)) (half-right (= mary 4) (= betty 1)))) (list (list 'betty betty) (list 'ethel ethel) (list 'joan joan) (list 'kitty kitty) (list 'mary mary))))))))))) The chain of let expressions above looks like an unrolled list. A more elegant solution would be to use a procedure that ambiguously returns a permutation of a list. (define (girls-results) (let ((permutation permutation-of '(1 2 3 4 5))) (map set! '(betty ethel joan kitty mary) permutation) (require ... as above ...