我正在解决 Haskell 中的 99 个问题并遇到了我无法解决的类型问题。我在第一次尝试时使用包装函数来解决问题。
目标
将列表元素的连续副本打包到子列表中。如果列表包含重复的元素,则应将它们放置在单独的子列表中。
例子:
Main> pack ['a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e']
["aaaa","b","cc","aa","d","eeee"]
我的代码:
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)
所以当我运行它时,我遇到了第 7 行的问题并得到以下调试器输出:
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.
我只是看不到额外的 [a0] -> [[a]] 来自哪里。
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]]
有什么东西在我头上。有人可以解释我错过了什么吗?