[plt-scheme] hash-has-key?
2009-03-28 Dave Herman <dherman at ccs.neu.edu>:
> [...]
>>>> Because Scheme is untyped, it's not possible for hash-ref to return one
>>>> standard "not found" value, so the API ought to provide a predicate.
>>
>> I'm not sure what this means. hash-ref accepts a thunk to call in the
>> case the result isn't found.
>
> Yes, certainly. My point wasn't deep, just that it isn't possible to create
> a collection API that returns a special "not found" value without the
> possibility of someone actually storing the "not found" value in the
> collection [...]
Hello,
I think it would actually be possible to design the collection API
such that it returns a single unambiguous "not found" value. All that
has to be done to ensure unambiguity is wrapping up "found" values:
(define (my-hash-ref ht key)
(let/ec return
(cons 'found (hash-ref ht key (curry return 'not-found)))))
With this definition of a hash lookup function 'not-found is returned
if and only if the hash table contains no mapping for the given key.
Whether this design is really a good solution is another question,
especially in a weakly typed language.
cu,
Thomas
--
When C++ is your hammer, every problem looks like your thumb.