開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: MIT/GNU Scheme
計算機プログラムの構造と解釈(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.3(評価器のデータ構造)、述語のテスト、手続きの表現、環境に対する操作、問題 4.13を解いてみる。
その他参考書籍
問題 4.13
コード(BBEdit)
sample.scm
;; evalにunbindを組み込む
(define (eval exp env)
(cond ((…))
;; …
((unbind! exp) (eval-unbinding exp env))
;; …
(else
(error "Unkown expression type -- EVAL" exp))))
(define (unbind? exp) (tagged-list? exp 'unbined!))
(define (unbind-variable exp) (cadr exp))
(define (eval-unbinding exp env)
(unbind! (unbind-variable exp)
env)
'ok)
;; 特殊形式unbind!の仕様
;; 環境の最初のフレームからだけ結合を除去する
(define (unbind! var env)
(let ((frame (first-frame env)))
(define (scan vars vals)
(cond ((null? vars)
(error "Unbound variable" var))
((eq? var (car vars))
(set-car! vars (cadr vars))
(set-cdr! vars (cddr vars))
(set-car! vals (cadr vals))
(set-cdr! vals (cddr vals)))
(else
(scan (cdr vars) (cdr vals)))))
(scan (frame-variables frame)
(frame-values frame))))
;; テスト用
(define f-1 (make-frame '(a b) (list 1 2)))
(define f-2 (make-frame '(a c d e) (list 3 4 5 6)))
(define e (list f-1 f-2))
入出力結果(Terminal, REPL(Read, Eval, Print, Loop))
1 ]=> e ;Value 2: (((a b) 1 2) ((a c d e) 3 4 5 6)) 1 ]=> (unbind! 'c e) ;Unbound variable c ;To continue, call RESTART with an option number: ; (RESTART 1) => Return to read-eval-print level 1. 2 error> ^C Interrupt option (? for help): ;Quit! 1 ]=> (unbind! 'a e) ;Unspecified return value 1 ]=> e ;Value 2: (((b) 2) ((a c d e) 3 4 5 6)) 1 ]=> (unbind! 'a e) ;Unbound variable a ;To continue, call RESTART with an option number: ; (RESTART 1) => Return to read-eval-print level 1. 2 error> e ;Value 2: (((b) 2) ((a c d e) 3 4 5 6))
0 コメント:
コメントを投稿