UPenn homework 3: histogram
This seems overwrought. Let's start with getCountElements.

It's often a good idea to consider not just the problem at hand, but a more general statement of what it is you're trying to solve. In this case, you're trying to get a frequency count of each distinct element in a list. At this point don't worry about the particulars of your problem (like the fact that you're only supposed to be counting Integers with values [0..9]), frequency counts are a well-defined problem for arbitrary data sets and as such lend themselves to a generalized solution.

So think about what a general solution would look like. Well, to start, your function will be handed a [a] -&gt;, just some list of arbitrary elements. But we need to group up which ones are equal to each other so that implies that we must have an equality constraint, (Eq a) =&gt;. After we find and count the equal elements you'll need to return that count, so -&gt; [Int]. But that doesn't make sense now given our generalized problem, the position of each Int in the list says nothing about which as it represents, so instead we'll return -&gt; [(a, Int)], a list of pairs of an element and the count of that element in the original list. Putting it all together we get&mdash;

frequencyCount :: (Eq a) =&gt; [a] -&gt; [(a, Int)]


Now we could make this work as-is&dagger;, but there's an important optimization we can make by tightening our constraints further. By first sorting the list we are given we can then easily group together identical elements, improving our asymptotic performance to O(n log n). Thus&mdash;

frequencyCount :: (Eq a, Ord a) =&gt; [a] -&gt; [(a, Int)]


Jumping back to your version before showing the implementation, let's compare what's going on between both already.

getCountOfElements :: [Integer] -&gt; ([Integer],Integer)


Program specifications and logic are threaded all through, from the specification of the domain to Integers, to the encoding of element identity in the position of the list in the return type, through to the inclusion of the largest count in the returned pair (a particularly odd design choice given the name of the function).

Getting back to implementing frequencyCount our implementation should be almost self-evident after having given the type. First we'll sort the elements to efficiently group the similar ones, then we'll run-length encode the list.

frequencyCount :: (Eq a, Ord a) =&gt; [a] -&gt; [(a, Int)]
frequencyCount = runLength . sort
    where
        runLength :: (Eq a) =&gt; [a] -&gt; [(a, Int)]
        runLength = map (\es -&gt; (head es, length es)) . group


Et, voilà! Let's now contrast some of the properties of the getCountOfElements implementation.


Due to the use of !! for indexing, if the [Integer] we're passed is out of bounds an exception will be thrown. Better filter the whole list of elements beforehand.
count uses take, drop, and of course !!, all of which are O(n) operations on Haskell lists. That's three traversals of the index-encoded list for each element, consider the worst case performer of getCountOfElements (replicate 100000 9). This would have to be cleaned up through use of splitAt.
Using ++ in count will walk the left portion of the accumulator list again, consider again that worst case performer.


In the face of all that, getCountOfElements is an O(n) algorithm (albeit with a very large constant factor) whereas frequencyCount is O(n log n). So despite the constructive beauty of frequencyCount, to do better than getCountOfElements I'll have to steal your algorithm and use it with an appropriate Haskell data structure for constant time indexing and updating.

Data.Array from the array package provides such a data structure for us, and in fact the very first visible code example in the documentation is for a histogram function.

hist :: (Ix a, Num b) =&gt; (a,a) -&gt; [a] -&gt; Array a b
hist bnds is = accumArray (+) 0 bnds [(i, 1) | i&lt;-is, inRange bnds i]


To use this version we have to provide a pair of bounds, in this question's particular case (0, 9).

count09Elements :: [Integer] -&gt; [(Integer, Int)]
count09Elements = assocs . hist (0, 9)


In conclusion, Haskell is a land of contrasts. So, we learned today that there are many different ways to skin a cat. Depending on what you know about your problem domain many different complexity classes of algorithms may be available to you. And don't use linked lists for operations requiring constant time updating.



Appendix

&dagger; An inefficient but important frequencyCount implementation. Note that performance is O(n&sup2;) (consider e.g., frequencyCount [0..100000]).

import Data.List (partition)

frequencyCount :: (Eq a) =&gt; [a] -&gt; [(a, Int)]
frequencyCount []       = []
frequencyCount xs@(e:_) = (head es, length es) : frequencyCount xs'
    where
        (es, xs') = partition (== e) xs