Finding a "Count Sequence"

Viewed 149

Given a list of integers xs, let:

count :: [Integer] -> Integer -> Integer
count xs n = length . filter (==n) $ xs

count the number of times the integer n occurs in the list.

Now, given a "list" (some sort of array of integers, can be something besides a List) of length n, write a function

countSequence :: [Integer] -> Integer -> Integer -> Integer
countSequence xs n m = [count xs x | x <- [0..m]]

that outputs the "list of counts" (0th index contains number of times 0 occurs in the list, 1st index contains number of times 1 occurs in the list, etc) that has time compleity o(m*n)

The above implementation I've given has complexity O(m*n). In Python (which I'm more familiar with), it's easy to do this in O(m + n) time --- iterate through the list, and each element increment a counter in some other list, which is initialized to be all zeros and length (m+1).

How could I get a better implementation in Haskell? I'd prefer if it wasn't some trivial way to implement the Python solution (such as adding another input to the function to keep the "list of counts" in and then interating through it).

3 Answers
Related