Chapter 4, Metalinguistic Abstraction

Exercise 4.42


Output:

1
2
3
4
5
6
7
8
9
10
11
;;; Amb-Eval input:
(liars-puzzle)
;;; Starting a new problem 
;;; Amb-Eval value:
ok
;;; Amb-Eval value:
((betty 3) (ethel 5) (joan 2) (kitty 1) (mary 4))
;;; Amb-Eval input:
try-again
;;; There are no more values of
(liars-puzzle)

Code:

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
(define (and a b)
  (if a
	  (if b true false)
	  false))
(define (or a b)
  (if a
	  true
	  (if b true false)))
(define (xor a b)
  (or (and a (not b))
	  (and (not a) b)))

(define (liars-puzzle)
  (let ((betty (amb 1 2 3 4 5))
		(ethel (amb 1 2 3 4 5))
		(joan (amb 1 2 3 4 5))
		(kitty (amb 1 2 3 4 5))
		(mary (amb 1 2 3 4 5)))
	(require (distinct? (list betty ethel joan kitty mary)))
	(require (xor (= kitty 2) (= betty 3)))
	(require (xor (= ethel 1) (= joan 2)))
	(require (xor (= joan 3) (= ethel 5)))
	(require (xor (= kitty 2) (= mary 4)))
	(require (xor (= mary 4) (= betty 5)))

	(list (list 'betty betty)
          (list 'ethel ethel)
          (list 'joan joan)
          (list 'kitty kitty)
          (list 'mary mary))))