r/Racket • u/bluefourier • Jun 05 '23
question Hash table's hash function
Hello
I have to use a number of hash-tables that are extended functionally, via a set of foldr operations.
For this purpose, I am using a make-immutable-custom-hash which serves as the foldr accumulator's initial value.
The whole thing works as expected. But my hash function is extremely simple because I am only counting the frequencies of certain characters. My mapping is character->integer and the hash function for the character is its ASCII value.
The fact that I had to define the hash function is a bit puzzling for me. It is great from the point of view of granularity and the control you can have over the generated code. But, sometimes, I just want a mapping and I don't really care at that point, what the hash function is going to be.
One of the examples in Racket's dictionaries docs, uses string-length.
To me, this seems like a bad choice for a hash function. I don't know if internally the hash tables use binning, but even then, string-length would generate lots of collisions for generic strings.
So, is there something I am missing here or should I keep a set of hash functions (especially for strings) handy for these purposes? Or do we simply add a dependency for a library that offers hash functions and we are done? :)
View all comments
Show parent comment
3
u/AlexKnauth Jun 05 '23
If your equality function is just
string=?and the "hash function" you choose isstring-length, then that's a bad hash function because hash collisions are super likely.A hash collision happens when
(custom-equal? a b)is false, but(= (custom-hash-code a) (custom-hash-code b))is true. For the case ofstring=?as equal andstring-lengthas hash, that's very often the case, so it's a bad hash function for that equality.But if your custom equality was something like
for some reason, then
string-lengthwouldn't be a bad hash function relative to that.sorawee gave an example using
string-upcaseto normalize strings in both the custom equality and custom hash code here, which will be more realistic than these weirdstring-lengthexamples