[Haskell-beginners] How would you improve this program?

Chaddaï Fouché chaddai.fouche at gmail.com
Sun Oct 9 23:40:21 CEST 2011


On Sun, Oct 9, 2011 at 10:11 PM, Lorenzo Bolla <lbolla at gmail.com> wrote:
> I've implemented 3 versions of the algorithm:
>
> a Haskell version using the standard "sort": read all the words from stdin,
> sort them and group them.
> a Haskell version using map: read all the words from stdin, stick each word
> in a Data.Map incrementing a counter if the word is already present in the
> map.

You seem to be using fromListWith which is lazy, in other words you're
accumulating enormous (1 + 1 + ... + 1) thunks in your map especially
with the higher word count tests. It might be that GHC is optimizing
that for you but I strongly doubt it. You should probably replace it
by a stricter version :

> fromListWith' f xs = foldl' ins empty xs
>     where ins m (k,x) = insertWith' f k x m

I hope that helps, otherwise it seems pretty good (but I only gave it
a passing glance).

-- 
Jedaï



More information about the Beginners mailing list