2013年9月14日土曜日

開発環境

計算機プログラムの構造と解釈(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.7(構文解析を実行から分離する)、問題 4.22を解いてみる。

その他参考書籍

問題 4.22

コード(BBEdit)

sample.scm

;; 評価器を拡張し、特殊形式letを使えるようにする
(define (analyze exp)
  (cond ((self-evaluating? exp)
         (analyze-self-evaluating exp))
        ((quoted? exp) (analyze-quoted exp))
        ((variable? exp) (analyze-variable exp)
        ((assignment? exp) (analyze-assignment exp))
        ((definition? exp) (analyze-defintion exp))
        ((if? exp) (analyze-if exp))
        (lambda? exp) (analyze-lambda exp))
        ;; letをlambdaから導出して追加
        ((let? exp) (analyze (let->lambda exp)))
        ((begin? exp) (analyze-sequence (begin-actions exp)))
        ((cond? exp) (analyze (cond->if exp)))
        ((application? exp) (analyze-application exp))
        (else
         (error "Unknown expression type -- ANALYZE" exp))))

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

(define (let-vars-and-exps exp)
  (cadr exp))

(define (let-body exp)
  (cddr exp))

(define (let-vars exp)
  (map car
       (let-vars-and-exps exp)))

(define (let-exps exp)
  (map cadr
       (let-vars-and-exps exp)))

(define (let->lambda exp)
  (cons (lambda (let-vars exp)
          (let-body exp))
        (let-exps exp)))

0 コメント:

コメントを投稿