[r6rs-discuss] Hash function return value constraints
per at bothner.com
Fri Apr 3 15:54:25 EDT 2009
On 04/03/2009 07:18 AM, Alex Queiroz wrote:
> On 4/3/09, leppie<xacc.ide at gmail.com> wrote:
>> The R6RS states:
>> "Hash-function should accept a key as an argument and should return a
>> non-negative exact integer object."
>> This only the only place that mentions a non-negative integer.
>> What is the rationale for making the hash value non-negative?
> Maybe because the output is supposed to be used as an index into
> some hash table array.
Huh? You can't use a hash value as an index into an array unless you
know the hash is less than the length of the array, which is rare.
You have to take the modulo of the hash value.
In which case a negative value works just fine. So yes, it makes
sense to allow negative hash values if that allows better use of fixnum.
Java hashCode returns a (32-bit signed) int, and that is what Kawa
uses and will continue to use for hash "protocol". Nothing else
would be sane on the JVM.
per at bothner.com http://per.bothner.com/
More information about the r6rs-discuss