(wat-aro)

無職から有職者にランクアップしました

SICP 問題 4.06

letを導入.
lambdaに変形することで定義する.

;; (let ((a 1) (b 2) (c 3))
;;   (+ a b c))

;; ((lambda (a b c)
;;    (+ a b c)) 1 2 3)

(define (let? exp) (tagged-list? exp 'let))
(define (let-parameters exp) (cadr exp))
(define (let-variables exp) (map car (let-parameters exp)))
(define (let-expressions exp) (map cadr (let-parameters exp)))
(define (let-bodys exp) (cddr exp))

(define (let->lambda exp)
  (cons (make-lambda (let-variables exp)
                     (let-bodys exp))
        (let-expressions exp)))
gosh> (let->lambda '(let ((var1 exp1)
                              (var2 exp2)
                              (var3 exp3))
                          body1
                          body2))
((lambda (var1 var2 var3)
   body1
   body2)
 exp1 exp2 exp3)
(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))
        ((let? exp) (eval (let->lambda exp) env)) ;;letを追加
        ((begin? exp)
         (eval-sequence (begin-actions exp) env))
        ((cond? exp) (eval (cond->if exp) env))
        ((and? exp) (eval (and->if exp) env))
        ((or? exp) (eval (or->if exp) env))
        ((application? exp)
         (apply (eval (operator exp) env)
                (list-of-values (operands exp) env)))
        (else
         (error "Unknown expression type: EVAL" exp))))