UPenn homework 3: histogram Please see here for the general description.
Exercise 3: histogram :: [Integer] -&gt; String
takes as input a list of Integers between 0 and 9 (inclusive),
and outputs a vertical histogram showing how many of each number
were in the input list.
histogram :: [Integer] -&gt; String
histogram xs = let
  (a,m) = getCountOfElements xs
  unpadded = getStrings a
  padded = pad m unpadded
  transposed = transp ([], padded)
  in mkOutput transposed

mkOutput :: [String] -&gt; String
mkOutput xss = let nums = &quot;0123456789\n&quot;
                    sep = &quot;==========\n&quot;
                    t = foldl (\a b -&gt; b++&quot;\n&quot;++a) &quot;&quot; xss
                in t ++ sep ++ nums

pad :: Integer -&gt; [String] -&gt; [String]
pad _ [] = []
pad n (xs:xss) = (xs ++ (replicate (fromInteger n - length xs) ' ')) : (pad n xss)

getCountOfElements :: [Integer] -&gt; ([Integer],Integer)
getCountOfElements ys =
  let arr = foldr (\e acc -&gt; count e acc) (replicate 10 0) ys
        where
          count :: Integer -&gt; [Integer] -&gt; [Integer]
          count n xs = let idx = fromInteger n
                       in (take idx xs) ++ [(xs!!idx + 1)] ++ (drop (idx+1) xs)
      maxElt = maximum arr
  in (arr, maxElt)

getStrings :: [Integer] -&gt; [String]
getStrings = map (\x -&gt; replicate (fromInteger x) '*')

-- good, descriptive name is lacking...
f :: [[a]] -&gt; ([a], [[a]])
f xss = foldr h ([],[]) xss where
  h [] (acc1, acc2) = (acc1, [])
  h (x:xs) (acc1, acc2) = (x:acc1, xs:acc2)

transp :: ([[a]],[[a]]) -&gt; [[a]]
transp (xs:xss, [] ) = reverse xss
transp (xss, yss) = let (r, l) = f yss
               in transp ([r] ++ xss, l)
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