2016/01/11
SICP 問題 4.39
制限の順番は解には影響しないが,その時間には影響する.
失敗が多い制限ほど先にテストするほうが実行速度は速くなる.
;; 本来のmultiple-dwelling
(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))
(sith (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 (< cooper miller))
(require (not (= (abs (- smith fletcher)) 1)))
(require (not (= (abs (- fletcher cooper)))))
(list (list 'baker baker)
(list 'cooper cooper)
(list 'fletcher fletcher)
(list 'miller miller)
(list 'smith smith))))
;; 改良版
(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))
(sith (amb 1 2 3 4 5)))
(require (distinct (list baker cooper fletcher miller smith)))
(require (< cooper miller))
(require (not (= (abs (- fletcher cooper)))))
(require (not (= (abs (- smith fletcher)) 1)))
(require (not (= fletcher 1)))
(require (not (= fletcher 5)))
(require (not (= baker 5)))
(require (not (= cooper 1)))
(list (list 'baker baker)
(list 'cooper cooper)
(list 'fletcher fletcher)
(list 'miller miller)
(list 'smith smith))))