I'm working through 99 Problems in Haskell and running into a type issue I cannot resolve. I was using a wrapper function to solve the problem on my first attempt.
The goal
Pack consecutive duplicates of list elements into sublists. If a list contains repeated elements they should be placed in separate sublists.
Example:
Main> pack ['a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e']
["aaaa","b","cc","aa","d","eeee"]
My code:
pack :: (Eq(a)) => [a] -> [[a]]
pack [] = []
pack xs = pack' ((filter (== head xs) xs):[]) (filter (/= head xs) xs)
pack' :: (Eq(a)) => [[a]] -> [a] -> [[a]]
pack' xs [] = xs
pack' xs ys = ((filter (== head ys) ys):xs) (filter (/= head ys) ys)
So when I run this, I have trouble with the 7th line and get the following debugger output:
09.hs:7:15:
Couldn't match expected type `[a0] -> [[a]]'
with actual type `[[a]]'
The function `(filter (== head ys) ys) : xs'
is applied to one argument,
but its type `[[a]]' has none
In the expression: ((filter (== head ys) ys) : xs) (filter (/= head ys) ys)
In an equation for pack':
pack' xs ys = ((filter (== head ys) ys) : xs) (filter (/= head ys) ys)
Failed, modules loaded: none.
I just do not see where the extra [a0] -> [[a]] is coming from.
Prelude> let b = [5,3,4,5,3,2,3,4,5,6]
Prelude> (filter (== head b) b):[]
[[5,5,5]]
Prelude> (filter (== head b) b):[[4,4]]
[[5,5,5],[4,4]]
Something is going over my head. Could someone explain what I am missing?
packis same asgroupfromData.List- Satvikgroupgroups only consecutive duplicates, fyi. - kqr