読者です 読者をやめる 読者になる 読者になる

SICPゼミ第42回

練習問題4.7
(define (let*->nested-lets exp)
  (let ((defs (car exp)) (body (cdr exp)))
    (if (null? (cdr defs))
      (list 'let (car defs) body)
      (list 'let (car defs)
        (let*->nested-lets (pair (cdr defs) body))))))

by dolicas

問題文で言ってる派生式でうまくいくはず.

練習問題4.8
(define (let->combination exp)
    (if (pair? (cadr exp))
        (let->combination-prototype exp)
        (let* ((defs (let-defs exp)) 
            (body (cadddr exp)) 
            (func (make-lambda defs body)))
          (let->combination-prototype
            (list 'let (cons ((cadr exp) func) defs) body))))

(define (let->combination-prototype exp)
    (let ((defs (let-defs exp)))
    ((make-lambda (let-variable-from-defs defs) (let-body exp))
     (let-defbody-from-defs defs))))
(define (let-defs exp)
    (cadr exp))
(define (let-body exp)
    (caddr exp))
(define (let-variable-from-defs defs)
        (if (null? defs)
            '()
            (cons (caar defs) (let-variable-from-defs (cdr defs)))))
(define (let-defbody-from-defs defs)
        (if (null? defs)
            '()
            (cons (cdar defs) (let-variable-from-defs (cdr defs)))))

by dolicas

練習問題4.9
(define (for? exp) (tagged-list? exp 'for))
(define (for-initial exp) cadr)
(define (for-terminate exp) caddr)
(define (for-body exp) cadddr)

(define (for->rec exp)
        (let->combination
          ('let for_iter
                ((i (for-initial exp)))
                (make-if
                  (> i (for-terminate exp))
                  'done
                  (begin (for-body exp)
                         (for_iter (+ i 1)))))))

by tube

練習問題4.10
(define (eval-if exp env)
(if (true? (eval (if-predicate exp) env))
(eval (if-alternative exp) env)
(eval (if-consequent exp) env)))

と変えるとifが逆になるという新しい文法のschemeになる.