Data.HashTable and duplicate keys

Simon Marlow simonmar at microsoft.com
Tue Sep 28 07:16:11 EDT 2004


On 28 September 2004 05:43, Ben Rudiak-Gould wrote:

> 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 (GHCi):
> 
> 	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. 

By all means go ahead and fix this, I won't have time to look into it in
the short term.

Cheers,
	Simon


More information about the Libraries mailing list