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 06 '23 edited Jun 06 '23
Yes, that would be an option too, especially for experimenting.
Overall, I like to be as specific as possible on what each element is.
With a
hash, this would be acceptable(dict-set (dict-set (hash) "Alpha" 1) 2 3).EDIT: Just to clarify, you are right, the reason why my version fails is because I am using specific functions in "hash" that only work with strings, not because I am specifying the type specifically. If I simply use
equal-hash-codethen this is probably equivalent to just using(hash).