]> git.rkrishnan.org Git - sicp.git/blobdiff - src/sicp/ex4_38.rkt
solutions to 4.38, 4.39 and 4.40
[sicp.git] / src / sicp / ex4_38.rkt
diff --git a/src/sicp/ex4_38.rkt b/src/sicp/ex4_38.rkt
new file mode 100644 (file)
index 0000000..e705ef0
--- /dev/null
@@ -0,0 +1,34 @@
+#lang racket
+
+(require "amb-eli.rkt")
+(require "distinct.rkt")
+
+(define (multiple-dwelling)
+  (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)])
+    (assert (distinct? (list baker cooper fletcher miller smith)))
+    (assert (not (= baker 5)))
+    (assert (not (= cooper 1)))
+    (assert (not (= fletcher 5)))
+    (assert (not (= fletcher 1)))
+    (assert (> miller cooper))
+    ;; (assert (not (= (abs (- smith fletcher)) 1)))
+    (assert (not (= (abs (- fletcher cooper)) 1)))
+    (list (list 'baker baker)
+          (list 'cooper cooper)
+          (list 'fletcher fletcher)
+          (list 'miller miller)
+          (list 'smith smith))))
+
+(collect (multiple-dwelling))
+
+#|
+'(((baker 1) (cooper 2) (fletcher 4) (miller 3) (smith 5))
+  ((baker 1) (cooper 2) (fletcher 4) (miller 5) (smith 3))
+  ((baker 1) (cooper 4) (fletcher 2) (miller 5) (smith 3))
+  ((baker 3) (cooper 2) (fletcher 4) (miller 5) (smith 1))
+  ((baker 3) (cooper 4) (fletcher 2) (miller 5) (smith 1)))
+|#
\ No newline at end of file