SICP 4.1 The Metacircular Evaluator
さわりだけ勉強して通り抜けようとしたけど、あきらめた。ちゃんと4章がんばる。処理系かけて一人前Lisperなら逃げない。
大事そうなのここ
1. To evaluate a combination (a compound expression other than a special form), evaluate the subexpressions and then apply the value of the operator subexpression to the values of the operand subexpressions.
2. To apply a compound procedure to a set of arguments, evaluate the body of the procedure in a new environment. To construct this environment, extend the environment part of the procedure object by a frame in which the formal parameters of the procedure are bound to the arguments to which the procedure is applied.
わかったような、わからないような。環境の中でlook up可能なシンボルと直接applyできるprimitiveな手続きになるまで、reduceしまくる必要があるのは分かった。日本語版SICP買ったのに、イラネって思って実家に送ってしまった。失敗。
Exercise 4.1 引数の評価順序の問題
最初の式がこれ
(define (list-of-values exps env)
(if (no-operands? exps)
'()
(cons (eval-print (first-operand exps) env)
(list-of-values (rest-operands exps) env))))
ますはgaucheで評価される順番を調べる。引数を表示して、引数を返すだけの関数を用意。2つめの引数yはlist-of-valuesのenv相当分。引数の数を合わせているだけ。
;;gaucheの実験
(define (eval-print x y)
(debug-print x)
x)
consを実行してみると
(cons (eval-print 'aa '()) (eval-print 'bb '()))
;#?=x
;#?- aa
;#?=x
;#?- bb
;(aa . bb)
こうなるので、左から右に評価されている。
次にlist-of-valuesで評価するために、この後で出てくる以下の手続きが必要。
(define (rest-operands ops) (cdr ops))
(define (first-operand ops) (car ops))
(define (no-operands? ops) (null? ops))
右から左に評価するには、いったんletで代入すればよい。
(define (list-of-values-inv exps env)
(if (no-operands? exps)
'()
(let ((rhs (list-of-values (rest-operands exps) env)))
(cons (eval-print (first-operand exps) env)
rhs))))
これで違いを確認できる。
(list-of-values '(a b) '())
(list-of-values-inv '(a b) '())
The comments to this entry are closed.


Comments
日本語わーらんすよ。
4 章読めるのはいつになるやら...
(特殊形式以外の合成形式である) 組み合わせを評価するには、
部分式を評価し、演算子の部分式の値を、被演算子の部分式の値に
作用させる。
合成手続きを一組の引数に作用させるには、
手続き本体を新しい環境で評価する。
この環境を構成するには、手続きオブジェクトの環境部分を、
手続きの仮パラメータが、手続きを作用させる引数に束縛される
フレームで拡張する。
Posted by: noboshemon | 2007.09.04 07:30 PM
noboshemonさん、こんにちは。
日本語の情報ありがとうございます。
aやtheの情報が欠落している分、日本語の方がわかりにくいですね。というか、2つめの方は、日本語として破綻している気もします。
「原書も読め」という無言のプレッシャーですね。
Posted by: なつたん | 2007.09.05 05:29 PM