I am trying to solve problem 10 from the ninety nine haskell problems. Here is my solution that I believe is correct.
The pack function (from question 9) is already correct. The problem is with the encode function.
pack :: (Eq a) => [a] -> [[a]]
pack [] = []
pack (x:xs) = (x : takeWhile (== x) xs) : (pack $ dropWhile (== x) xs)
encode :: (Eq a) => [a] -> [(Int, a)]
encode [] = []
encode list = (encode' $ head packed) : (encode $ tail packed)
where packed = pack list
encode' l = (length l, head l)
When I load the file from ghci, this is the error:
encode.hs:6:0: Occurs check: cannot construct the infinite type: a = [a] When generalising the type(s) for `encode'
Line 6 is the line containing encode [] = []
What's wrong with my encode function? I keep checking the types of the variables used and I believe there is nothing wrong.
Function usage example (assuming code is working correctly):
pack "aaaabbbbccccccddddddd"
> ["aaaa","bbbb","cccccc","ddddddd"]
encode "aaaabbbbccccccddddddd"
> [(4,'a'),(4,'b'),(6,'c'),(7,'d')]