2015/12/18

SICP 問題 4.02

``````;; 本文のeval
(define (eval exp env)
(cond ((self-evaluating? exp) exp)
((variable? exp) (lookup-valiable-value exp env))
((quoted? exp) (text-of-quotation exp))
((assignment? exp) (eval-assignment exp env))
((definition? exp) (eval-definition exp env))
((if? exp) (eval-if exp env))
((lambda? exp) (make-procedure (lambda-parameters exp)
(lambda-body exp)
env))
((begin? exp)
(eval-sequence (begin-actions exp) env))
((cond? exp) (eval (cond->if exp) env))
((application? exp)
(apply (eval (operator exp) env)
(list-of-values (operands exp) env)))
(else
(error "Unknown expression type: EVAL" exp))))

;; evalのcondの順番を変えてapplyをassignmentより前にしようとしている
;; a
(define x 3)
;; (application? exp)は(pair? exp)なので(define x 3)もapplyされる．
;; applyの節はすべてのリストにマッチするので最後でないといけない．

;; b
;; 手続き適用がcallで始まるように構文を変える．
;; Louisの希望通り，assignmentよりも前にapplyをもってくる．
;; 後はselectorとapplication?を変更すればよい．
(define (eval exp env)
(cond ((self-evaluating? exp) exp)
((variable? exp) (lookup-valiable-value exp env))
((quoted? exp) (text-of-quotation exp))
((application? exp)
(apply (eval (operator exp) env)
(list-of-values (operands exp) env)))
((assignment? exp) (eval-assignment exp env))
((definition? exp) (eval-definition exp env))
((if? exp) (eval-if exp env))
((lambda? exp) (make-procedure (lambda-parameters exp)
(lambda-body exp)
env))
((begin? exp)
(eval-sequence (begin-actions exp) env))
((cond? exp) (eval (cond->if exp) env))
(else
(error "Unknown expression type: EVAL" exp))))

(define (application? exp) (tagged-list? exp 'call))