[plt-scheme] Okay, this is ugly

From: Chongkai Zhu (u0476504 at utah.edu)
Date: Sat Feb 18 09:46:24 EST 2006


(define (env-get-binding s env)
  (if (null? env)
      (void) ;;or, (error ...)
      (hash-table-get (car env)
                      (lambda ()
                        (env-get-binding s (cdr env))))))

Chongkai Zhu  

======= At 2006-02-18, 01:13:12 Gregory Woodhouse wrote: =======

>The idea here is that env is a list of hash tables (created, for  
>example, by let), and when looking up a symbol, I want to start with  
>the car of env and return the binding I find there (if there is one).  
>Otherwise, I want to keep looking by repeating the process using the  
>cdr of env, until it's empty.
>The following code seems to work, but it's ugly because it uses the  
>failure thunk of hash-table-get to return #<void> if no binding is  
>found, and then uses this as a kind of sentinel value to indicate  
>that the code should recur on the list.
>(define (env-get-binding s env)
>   (call/cc
>    (lambda (k)
>      (if (null? env) k (void))
>      (let ((t (hash-table-get (car env) s (lambda () (void)))))
>        (unless (void? t) (k t)
>          (env-get-binding s (cdr env)))))))
>Using sentinel values like this seems unidiomatic and rather awkward.  
>Surely, there is a cleaner (clearer?) way to do this.
>Gregory Woodhouse
>gregory.woodhouse at sbcglobal.net
>"If you give someone Fortran, he has Fortran.
>If you give someone Lisp, he has any language he pleases."
>--Guy L. Steele, Jr.
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.