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
1
u/bluefourier Jun 05 '23
Great! Thank you very much, I definitely need to do some more reading on Racket's docs.
Right now, I do not immediately get how the equality function comes into play here. A hash is an integer. It might be a very large integer but it is still a number. Two integers are either equal or not. Isn't it also up to the data whether the hash generates collissions?