2013年8月27日火曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の4(超言語的抽象)、4.1(超循環評価器)、4.1.2(式の表現)、問題 4.4を解いてみる。

その他参考書籍

問題 4.4

コード(BBEdit)

sample.scm

;; 構文手続き
(define (and? exp) (tagged-list? exp 'and))

(define (or? exp) (tagged-list? exp 'or))

;; 評価手続き
(define (eval-and exp env)
  (if (null? exp)
      'true
      (if (true? (eval (cadr exp) env))
          (eval-and (cddr exp) env)
          'false)))

(define (eval-or exp env)
  (if (null? exp)
      'false
      (if (true? (eval (cadr exp) env))
          'true
          (eval-or (cddr exp) env))))

;; 組み込む
(define (eval exp env)
  (cond (…)
        ;; …
        ((and? exp) (eval-and exp env))
        ((or? exp) (eval-or exp env))
        ;; …
        ))

;; 導出された式として評価する方法

(define (and? exp) (tagged-list? exp 'and))

(define (and-predicates exp) (cdr exp))

(define (and->if exp)
  (expand-and-predicates (and-predicates exp)))

(define (expand-and-predicates predicates)
  (if (null? predicates)
      'true
      (let ((first (car predicates))
            (rest (cdr predicates)))
        (make-if first 
                 (expand-and-predicates rest)
                 'false))))

(define (or? exp) (tagged-list? exp 'or))

(define (or-predicates exp) (cdr exp))

(define (or->if exp)
  (expand-or-predicates (or-predicates exp)))

(define (expand-or-predicates predicates)
  (if (null? predicate)
      'false
      (let ((first (car predicates))
            (rest (cdr predicates)))
        (make-if first
                 'true
                 (expand-or-predicates rest)))))

;; 組み込む
(define (eval exp env)
  (cond (…)
        ;; …
        ((and? exp) (eval (and->if exp) env))
        ((or? exp) (eval (or->if exp) env))
        ;; …
        ))

0 コメント:

コメントを投稿