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

From: Eli Barzilay (eli at barzilay.org)
Date: Sat Feb 25 19:28:24 EST 2006

On Feb 25, Gregory Woodhouse wrote:
> 
> On Feb 25, 2006, at 3:43 PM, Ryan Culpepper wrote:
> 
> > You aren't actually applying the continuation in the failure thunk
> 
> Well, here's a simple case
> 
>  > (define x 4)
>  > (let ((y (call/cc (lambda (k) (k 0))))) (set! x 5))
>  > x
> 5
> 
> I had expected that the body of let statement would not have been
> executed, so x would still be 4. But I suppose it's a matter of
> strictness.

Strictness has nothing to do with this.  (Not much, at least...)
You use `call/cc' to grab a continuation and immediately use that
continuation, so the result of that whole thing is just 0.  In
general,

  (call/cc (lambda (k) (k E)))

is just a fancy way of writing E.


Back to your original problem, you don't need any kind of continuation
if you wrap the whole call in a `with-handlers':

  (define (hash-contains? t x)
    (with-handlers ([exn:fail:contract? (lambda (_) #f)])
      (hash-table-get t x)
      #t))

But you can also get away with no continuations and no handlers -- you
only need a unique identifiable value:

  (define hash-contains?
    (let ([secret (list 'whatever)])
      (lambda (t x)
        (not (eq? secret (hash-table-get t x (lambda () secret)))))))

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.