I'd use a Map in GHC 6.4:
count xs = toList $ fromListWith (+) (zip xs (repeat 1))
or a FiniteMap in earlier versions:
count xs = fmToList $ addListToFM_C (+) emptyFM (zip xs (repeat 1))
both of these seem to be quite fast.

 - Cale

On 4/28/05, Dmitry Vyal <[EMAIL PROTECTED]> wrote:
> Hello everybody.
> 
> I have a long list consisted of a small number (about a dozen) of
> elements repeating in random pattern. I know all the possible elements.
> I need to count number of occurences of each particular element and to
> do i quickly.
> 
>     For example
> quick_func Eq a => [a] -> [(a,Int)]
> quick_func [1,2,3,1,2,9,1,9] == [(1,3),(2,2),(3,1),(9,2)]
> 
> According to profiler this function is the bottle-neck in my sluggish
> program so I really need to speed it up.
> 
> Any proposals?
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to