SICP Exercise 4.9

SICP Exercise 4.9

;(for 
;  (define i 0)
;  (< i 10)
;  (set! i (+ i 1))
;  (display i))

;(for <init> <condition> <change> <body>)
(define (for-init exp) (cadr exp))
(define (for-condition exp) (caddr exp))
(define (for-change exp) (cadddr exp))
(define (for-body exp) (caddddr exp))
(define (caddddr x) (car (cddddr x)))

(define (for->combination exp)
  (sequence->exp
   (list
    (for-init exp)
    (list 'define
          (list 'for-iter)
          (make-if (for-condition exp)
                   (sequence->exp
                    (list (for-body exp)
                          (for-change exp)
                          (list 'for-iter)))
                   "undefine-FOR"))
    (list 'for-iter))))

(define (eval-for exp env)
  (eval (for->combination exp) env))

(put 'for eval-for)
    
  
  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章