
On 2005-10-25 at 12:20+0200 Lemmih wrote:
On 10/25/05, Charles SDudu
wrote: Hello, I need to calculate the frequency of each character in a String. And if I can do this really well in C, I dont find a nice (and fast) answer in haskell. I tried several functions, listed below, and even the fastest do a lot of unnecessary things :
calc :: String -> [ (Char, Int) ] calc = filter (\p -> snd p > 0) . assocs . foldl (\k c -> unsafeReplace k [(fromEnum c, (unsafeAt k (fromEnum c))+1)] ) k where k = array (toEnum 0, toEnum 255) [(toEnum i, 0) | i <- [0 .. 255]] :: UArray Char Int
[snip even more disagreable code] Ugh! These are all horrid. If something on the lines of
calc = accumArray (+) 0 (minBound, maxBound) . (map (\x->(x,1)))
isn't fast enough, complain to the implementors! What's the point of functional programming if one has to twist into a shape that allows inspection of one's own fundament to get stuff to run in decent time? Jón -- Jón Fairbairn Jon.Fairbairn at cl.cam.ac.uk