[plt-scheme] hash-has-key?

From: Henk Boom (henk at henk.ca)
Date: Fri Mar 27 19:14:21 EDT 2009

Hi, I was looking for a way to test whether or not a hash table has a
mapping for a given key. I was surprised not to find anything in the
docs, but thought of two ways to implement this:

(define (hash-has-key? hash key)
  (let/cc return
    (hash-ref hash key (lambda () (return #f)))

(define hash-has-key?
  (let ((distinct-value (gensym)))
    (lambda (hash key)
      (not (eq? distinct-value (hash-ref hash key distinct-value))))))

Both of these seem like roundabout approaches, is there a built-in way
to do this? If not, is one of these better than the other?


Posted on the users mailing list.