2014年10月12日日曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の4(超言語的抽象)、4.3(Schemeの変形 - 非決定性計算)、4.3.3(非決定性プログラムの例)、論理パズル、問題 4.40.を解いてみる。

その他参考書籍

問題 4.40.

解の割当が相異なるという要求をする前は、5^5 = 3125、要求する後は 5! = 120。

コード(BBEdit, Emacs)

sample40.scm

#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-

(define (multiple-dwelling)
  (let ((fletcher (amb 1 2 3 4 5)))
    (require (not (= fletcher 1)))
    (require (not (= fletcher 5)))
    (let ((cooper (amb 1 2 3 4 5)))
      (require (not (= cooper 1)))
      (require (not (= (abs (- fletcher cooper)) 1)))
      (require (distinct? fletcher cooper))
      (let ((baker (amb 1 2 3 4 5)))
        (require (not (= baker 5)))
        (require (distinct? fletcher cooper baker))
        (let ((miller (amb 1 2 3 4 5)))
          (require (> miller cooper))
          (require (distinct? fletcher cooper baker miller))
          (let ((smith (amb 1 2 3 4 5)))
            (require (not (= (abs (- smith fletcher) 1))))
            (require (distinct? fletcher cooper baker miller smith))
            (list (list 'baker baker)
                  (list 'cooper cooper)
                  (list 'fletcher fletcher)
                  (list 'miller miller)
                  (list 'smith smith))))))))

0 コメント:

コメントを投稿