[Haskell-cafe] Solution to Thompson's Exercise 4.4

Mark Carroll markc at chiark.greenend.org.uk
Sat Mar 12 08:14:01 EST 2005

I had a go with things along this theme and came up with a couple of
options, with different type signatures. I use some functions from the
Data.List library.

If we know that, as with Ints, we are dealing with list members that are
instances of Ord, we can do:

howManyEqual :: (Eq a, Ord a) => [a] -> Int

howManyEqual = maximum . (0 :) . map length . group . sort

Otherwise, we end up less efficient, with:

howManyEqual :: Eq a => [a] -> Int

howManyEqual = countEach 0
    countEach best []         = best
    countEach best list@(x:_) =
        let (xs, others) = partition (== x) list
         in countEach (max (length xs) best) others

-- Mark

More information about the Haskell-Cafe mailing list