[plt-scheme] hash-has-key?

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Sat Mar 28 11:17:42 EDT 2009

On Sat, Mar 28, 2009 at 11:11 AM, Thomas Chust <chust at web.de> wrote:
> 2009-03-28 Carl Eastlund <carl.eastlund at gmail.com>:
>> On Sat, Mar 28, 2009 at 10:43 AM, Thomas Chust <chust at web.de> wrote:
>>> [...]
>>>  (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.
>>
>> Types have nothing to do with the inconvenience here; typed or
>> untyped, the result of a successful lookup has to be unwrapped here.
>> [...]
>
> Hello Carl,
>
> in a statically typed language the compiler can automate or aid the
> unwrapping process more easily. But you are right that this is more of
> an indirect effect and it's also a matter of taste how much magic a
> compiler should apply :-)

I thought you might bring that up.  (1) In an untyped language, the
compiler can still do this kind of thing.  For instance, in Perl,
strings and numbers are freely converted, and lists are freely spliced
together.  Automatic coercions do not require static types.  (2) This
would go back to the original problem.  If you wrapped up the
'not-found value in the 'found wrapper, the automatic unwrapping would
put you back in the situation where you couldn't tell if 'not-found
meant no value was found, or 'not-found was the value in the table,
wrapped up in 'found, then automatically unwrapped.

-- 
Carl Eastlund


Posted on the users mailing list.