Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Haskell hash implementation that does not live in the IO monad

I am looking for a data structure that works a bit like Data.HashTable but that is not encumbered by the IO monad. At the moment, I am using [(key,val)]. I would like a structure that is O(log n) where n is the number of key value pairs.

The structure gets built infrequently compared to how often it must be read, and when it is built, I have all the key value pairs available at the same time. The keys are Strings if that makes a difference.

It would also be nice to know at what size it is worth moving away from [(key,val)].

like image 280
John F. Miller Avatar asked Apr 26 '11 23:04

John F. Miller


People also ask

What is IO Monad in Haskell?

The I/O monad contains primitives which build composite actions, a process similar to joining statements in sequential order using `;' in other languages. Thus the monad serves as the glue which binds together the actions in a program.

How is Haskell IO implemented?

It's implemented using unsafeInterleaveIO , which does trickery behind the scenes to allow lazy I/O. It's not a good example of how IO is supposed to work.

How is Haskell IO pure?

Haskell is a pure language Being pure means that the result of any function call is fully determined by its arguments. Procedural entities like rand() or getchar() in C, which return different results on each call, are simply impossible to write in Haskell.


2 Answers

You might consider:

  • Data.Map

or alternatively,

  • Data.HashMap

The former is the standard container for storing and looking up elements by keys in Haskell. The latter is a new library specifically optimized for hashing keys.

Johan Tibell's recent talk, Faster persistent data structures through hashing gives an overview, while Milan Straka's recent Haskell Symposium paper specifically outlines the Data.Map structure and the hashmap package.

like image 119
Don Stewart Avatar answered Oct 27 '22 01:10

Don Stewart


If you have all the key-value pairs up front you might want to consider a perfect hash function.

Benchmarking will tell you when to switch from a simple list.

like image 24
augustss Avatar answered Oct 26 '22 23:10

augustss