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

SICPゼミ第43回

練習問題4.11

(define (make-frame variables values)
  (if (= (length variables values))
      (if (null? variables)
          nil
         (cons (cons (car variables) (car values))) (make-frame (cdr variables) (cdr values)))
      (if (< (length variables) (length values))
          (error "Too many arguments supplied" variables values)
          (error "Too few arguments supplied" variables values ))))

(define (frame-variables frame)
  (if (null? frame)
      nil
      (cons (caar frame) (frame-variables (cdr frame)))))
(define (frame-values frame)
  (if (null? frame)
      nil
      (cons (cdar frame) (frame-variables (cdr frame)))))

(define (add-binding-to-frame! var val frame)
  (set! frame (cons (cons var val) frame)))

by pine

練習問題4.12
(define (enviroment-searching-alg var find-operation error-operation env)
  (define (env-loop env)
    (define (scan vars vals)
      (cond ((null? vars)
        (env-loop (enclosing-environment env)))
        ((eq? var (car vars)) (find-operation (car vals) (car vars)))
        (else (scan (cdr vars) (cdr vals)))))
    (if (eq? env the-empty-environment)
      (error-operation)
      (let ((frame (first-frame env)))
        (scan (frame-variables frame)
        (frame-values frame)))))
  (env-loop env))

(define (lookup-variable-value var env)
  (enviroment-searching-alg var (lambda (x y) x) (error 'hoge) env))

(define (set-variable-value! var val env)
  (enviroment-searching-alg var (lambda (x y) (set! x val)) (error 'hoge) env))

(define (define-variable! var val env)
  (enviroment-searching-alg var (lambda (x y) (set! x val)) 
    (add-binding-to-frame! var val (car env)) (list (car env))))

by dolicas

練習問題4.13
(define (make-unbound! var env) 
    (define (env-loop env) 
        (define (scan vars vals) 
            (cond ((null? vars) (env-loop (enclosing-environment env))) 
                    ((eq? var (car vars))
                     (begin (set! vals (cdr vals))
                              (set! vars (cdr vars)))) 
                    (else (scan (cdr vars) (cdr vals))))) 
        (if (eq? env the-empty-environment) 
             (error "Unbound variable" var) 
             (let ((frame (first-frame env))) 
                    (scan (frame-variables frame) 
                            (frame-values frame))))) 
    (env-loop env))

By tube