【发布时间】:2021-07-19 00:29:21
【问题描述】:
问题是:
“列表的行程编码。使用problem P09的结果来实现所谓的run-length encoding数据压缩方法。元素的连续重复被编码为列表(N E),其中 N 是元素 E 的重复数。”
预期结果是:
λ> encode "aaaabccaadeeee"
[(4,'a'),(1,'b'),(2,'c'),(2,'a'),(1,'d'),(4,'e')]
我已经创建了这段代码:
encode [] = []
encode (x:xs) = (counting xs,x) : encode (dropWhile (==x) xs )
where counting (y:ys)
| y == head ys = 1 + counting ys
| otherwise = 0
repl 说:
`<interactive>:(1,1)-(5,22): Non-exhaustive patterns in function encode`
我不知道我的递归错误在哪里。
【问题讨论】:
-
counting []的值应该是多少? -
我的思维方式有缺陷。谢谢,我意识到了我的错误,现在我在 haskell 中做得更好了。
-
始终保持打开警告。以这种方式丢失的
[]情况由编译器在代码运行之前报告。 -
我在 jupyter notebook 上运行 haskell,简单易学。
-
他们可能只是想重用之前问题 9 中的函数,并使用 1) 该函数和 2) 库函数
map解决问题 10。
标签: haskell run-length-encoding non-exhaustive-patterns