haskell - Couldn't match expected type in Pack function -
i'm working through 99 problems in haskell , running type issue cannot resolve. using wrapper function solve problem on first attempt.
the goal
pack consecutive duplicates of list elements sublists. if list contains repeated elements should 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 run this, have trouble 7th line , following debugger output:
09.hs:7:15: couldn't match expected type `[a0] -> [[a]]' actual type `[[a]]' function `(filter (== head ys) ys) : xs' applied 1 argument, type `[[a]]' has none in expression: ((filter (== head ys) ys) : xs) (filter (/= head ys) ys) in equation pack': pack' xs ys = ((filter (== head ys) ys) : xs) (filter (/= head ys) ys) failed, modules loaded: none. i not see [a0] -> [[a]] 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 going on head. explain missing?
this seventh line little weird:
((filter (== head ys) ys):xs) (filter (/= head ys) ys) what says is:
take function given by
((filter (== head ys) ys):xs)and call argument
(filter (/= head ys) ys)
which not @ intended. becomes more clear if replace expressions names, following equivalent expression:
let func = ((filter (== head ys) ys):xs) arg = (filter (/= head ys) ys) in func arg did miss put between 2 expressions? keep in mind arg in case [a] while func [[a]]. think meant say
func : [arg] but i'm not sure, because don't know trying accomplish.
Comments
Post a Comment