[plt-scheme] Checking to see if a key is present in a hash table

From: Doug Orleans (dougorleans at gmail.com)
Date: Sun Feb 26 13:38:47 EST 2006

Dave Herman writes:
 > It's easier than you're making it out to be. Don't bother with 
 > continuations. Just use an exception handler.
 > (define (bound? t s)
 >    (with-handlers ([exn? (lambda (exn) #f)])
 >      (hash-table-get t s)
 >      #t))

I'm not sure this is right.  Does `exn?' include break exceptions?  I
think it does, but I'm not going to look it up because I think it's a
bad idea to get involved with exceptions rather than Gregory's
original idea of using the failure-thunk to do a non-local return

   (define (bound? t s)
     (let/ec return
       (hash-table-get t s (lambda () (return #f)))

Providing a failure-thunk argument for `hash-table-get' seems like a
more robust way to use it than relying on it throwing a particular
(class of) exception.  And using an escape continuation also feels
more elegant to me than the sentinel-value solution, which always
seemed like kind of a dirty trick.  (I have no idea which is more
efficient, but I would hope it's not a big difference either way.)

--dougorleans at gmail.com

P.S. Note that you could replace the `#t' on the last line with
`(return #t)', if you prefer regularity over conciseness.

Posted on the users mailing list.