[Haskell-cafe] Nice way to calculate character frequency in a string

Jon Fairbairn Jon.Fairbairn at cl.cam.ac.uk
Tue Oct 25 08:02:38 EDT 2005

On 2005-10-25 at 13:42+0200 "Charles SDudu" wrote:
> okay, it took me some time to make your solution work, but it's as fast as 
> the precedent solution, and really more elegant. Thank you, but I dont know 
> how to structure the function, see yourself :
> calc str = filter (\p -> snd p > 0) $ assocs $ ((accumArray (+) 0 (toEnum 0, 
> toEnum 255) (map (\x->(x,1)) str)) :: UArray Char Int)
> It's not so nice but it works, isn't there a solution to put the type 
> elsewhere, and another type than UArray ?

Well, you can do this:

   import Array

   calc::String->[(Char, Int)]

   calc = filter (\p-> snd p > 0) 
	  . assocs 
	  . accumArray (+) 0 (minBound, maxBound) 
	  . (map (\x->(x,1)))

You can import whatever sort of array you want, so long as
it has assocs and accumArray.  You don't want to change
minBound and maxBound to toEnum x, since they are the bounds
of Char, which might or might not be 0 and 255.

Jón Fairbairn                              Jon.Fairbairn at cl.cam.ac.uk

More information about the Haskell-Cafe mailing list