Data.HashTable and duplicate keys

Ben Rudiak-Gould benrg at
Tue Sep 28 00:42:58 EDT 2004

On Mon, 27 Sep 2004, Einar Karttunen wrote:

> I recently noticed that Data.HashTable silently
> accepts duplicate keys and keeps all the values.
> This is very counterintuitive from the interface
> and makes updating a hashtable quite tedious 
> (lookup + delete + insert). 

OCaml's Hashtbl also works this way. It makes sense if you think of a hash
table as a speedier drop-in replacement for an association list:
Data.HashTable.insert behaves like consing a new (key,val) pair onto the
front of an association list, and Data.HashTable.delete and
Data.HashTable.lookup behave like the corresponding list functions. I
agree 100% that it should be documented, of course.

However, playing around with HashTable just now, I noticed the following

	Prelude> hash <- Data.HashTable.fromList id [(1,'a'),(1,'b')]
	Prelude> Data.HashTable.lookup hash 1 >>= print
	Just 'b'

This is clearly wrong, and makes me think that Data.HashTable's behavior
given identical keys wasn't well thought out. I'm in favor of plagiarizing
OCaml's Hashtbl interface, which has the update function (called replace)
as well as other useful operations like copy and find_all.

-- Ben

More information about the Libraries mailing list