私は 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]]
何かが私の頭の上を進んでいます。誰かが私が欠けているものを説明できますか?