From: Ramakrishnan Muthukrishnan Date: Sun, 6 Jan 2013 12:06:36 +0000 (+0530) Subject: solution to 4.41 and 4.42 X-Git-Url: https://git.rkrishnan.org/pf/content/en/service/status?a=commitdiff_plain;h=28e6afb9776cf53cf87d1e8e98e640f4bbd2d7d3;p=sicp.git solution to 4.41 and 4.42 --- diff --git a/src/sicp/ex4_41.rkt b/src/sicp/ex4_41.rkt new file mode 100644 index 0000000..ba4b97b --- /dev/null +++ b/src/sicp/ex4_41.rkt @@ -0,0 +1,24 @@ +#lang racket +(require "distinct.rkt") + +(define (multiple-dwelling) + (for/list ([baker '(1 2 3 4 5)] + #:unless (= baker 5) + [cooper '(1 2 3 4 5)] + #:unless (= cooper 1) + [fletcher '(1 2 3 4 5)] + #:unless (or (= fletcher 1) + (= fletcher 5)) + [miller '(3 4 5)] + #:when (> miller cooper) + [smith '(1 2 3 4 5)] + #:when (and (not (= (abs (- smith fletcher)) 1)) + (not (= (abs (- fletcher cooper)) 1)) + (distinct? (list baker cooper fletcher miller smith)))) + (list (list 'baker baker) + (list 'cooper cooper) + (list 'fletcher fletcher) + (list 'miller miller) + (list 'smith smith)))) + +(multiple-dwelling) \ No newline at end of file diff --git a/src/sicp/ex4_42.rkt b/src/sicp/ex4_42.rkt new file mode 100644 index 0000000..76ecad5 --- /dev/null +++ b/src/sicp/ex4_42.rkt @@ -0,0 +1,31 @@ +#lang racket + +(require "amb-eli.rkt") +(require "distinct.rkt") + +(define (xor x y) + (cond [(and x y) #f] + [(and (not x) (not y)) #f] + [else #t])) + +(define (liars) + (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)]) + (assert (distinct? (list betty ethel joan kitty mary))) + (assert (xor (= kitty 2) (= betty 3))) + (assert (xor (= ethel 1) (= joan 2))) + (assert (xor (= joan 3) (= ethel 5))) + (assert (xor (= kitty 2) (= mary 4))) + (assert (xor (= mary 4) (= betty 1))) + (list (list 'betty betty) + (list 'ethel ethel) + (list 'joan joan) + (list 'kitty kitty) + (list 'mary mary)))) + +(collect (liars)) + +;; '(((betty 3) (ethel 5) (joan 2) (kitty 1) (mary 4)))