sicp-ex-5.41



<< Previous exercise (5.40) | Index | Next exercise (5.42) >>


meteorgan

  
  
 (define (find-variable variable lst) 
   (define (search-variable v l n) 
     (cond ((null? l) false) 
           ((eq? v (car l)) n) 
           (else (search-variable v (cdr l) (+ n 1))))) 
   (define (search-frame frames f) 
     (if (null? frames) 
         'not-found 
         (let ((o (search-variable variable (car frames) 0))) 
              (if o 
                  (cons f o) 
                  (search-frame (cdr frames) (+ f 1)))))) 
   (search-frame lst 0)) 
            

donald

  
  
  
 (define (find-variable var ct-env) 
   (let ((f-ref 0)) 
     (define (find var frame) 
       (if (eq? var (car frame)) 
           0 
           (+ 1 (find var (cdr frame))))) 
     (define (iter ct-env) 
       (cond ((null? ct-env) 
              'not-found) 
             ((memq? var (car ct-env)) 
              (list f-ref (find var (car ct-env)))) 
             (else (set! f-ref (+ 1 f-ref)) 
                   (iter (cdr ct-env))))) 
     (iter ct-env)))