我想尝试一些haskell,并开始有99个haskell问题,目前我卡在13:列表的运行长度编码(尽管我的问题是更一般的)Haskell不会匹配列表
--datatype
data En a = Multiple (Int, a) | Single a | Empty
deriving (Show)
--helper
getnum :: (Eq a9) => [a9] -> Int
getnum [] = 0
getnum (x:xs)
| x == head xs = 1 + getnum xs
| otherwise = 1
encodeDirect :: [a] -> [En a]
endoceDirect [email protected](x:xs)
| getnum xxs == 1 = Single x : encodeDirect xs
| otherwise = Multiple ((getnum xxs), x) : encodeDirect (drop (getnum xxs) xxs)
encodeDirect _ = [Empty]
这应该给我最后一个[空]的解决方案,但是当我用ghci调用任何种类的列表/字符串的encodeDirect时,它会直接通过,我得到的只是[Empty]。
为什么[email protected](x:xs)
不符合任何列表?
Typo。你换了'c'和'd'。 –