2016年1月28日木曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs(Text Editor)
  • Scheme (プログラミング言語)
  • Gauche, kscheme,(github) (処理系)

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の第4章(超言語的抽象)、4.1(超循環評価器)、4.1.1(評価器の中核)、問題4.1.を取り組んでみる。

その他参考書籍

問題4.1.

コード(Emacs)

(define (print x) ((lambda () (display x) (newline))))
(define no-operands? null?)
(define first-operand car)
(define rest-operands cdr)

;; 環境についてはまだ未実装

(define n 1)
(define exps '(n (set! n (+ n 1))))

;; left to right
(define (list-of-values exps env)
  (if (no-operands? exps)
      '()
      ((lambda (left)
         (cons left
               (list-of-values (rest-operands exps) env)))
       (eval (first-operand exps) env))))

;; (1 2)
(print (list-of-values exps (interaction-environment)))

(set! n 1)
;; right to left
(define (list-of-values exps env)
  (if (no-operands? exps)
      '()
      ((lambda (right)
         (cons (eval (first-operand exps) env)
               right))
       (list-of-values (rest-operands exps) env))))

;; (2 2)
(print (list-of-values exps (interaction-environment)))

入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))

$ gosh sample1.scm
(1 2)
(2 2)
$

0 コメント:

コメントを投稿