[plt-scheme] hash-has-key?
On Fri, Mar 27, 2009 at 7:14 PM, Henk Boom <henk at henk.ca> wrote:
> 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)))
> #t))
>
> (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?
>
> Henk
There's no built-in way that I know if. When I write it myself, I
write almost the same as your first one, but I use let/ec instead of
let/cc to use an escape continuation -- I believe it's much more
efficient.
--
Carl Eastlund