我正在研究Haskell的99个问题(https://wiki.haskell.org/99_questions/1_to_10),我对问题#8有疑问.
8 Problem 8
(**) Eliminate consecutive duplicates of list elements.
If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed.
Run Code Online (Sandbox Code Playgroud)
我用foldr函数成功解决了这个问题.
compress :: Eq e => [e] -> [e]
compress = let f v [] = [v]
f v acc
| head acc == v = acc
| otherwise = v:acc
in foldr f []
Run Code Online (Sandbox Code Playgroud)
但是当我尝试用这样的递归解决同样的问题时:
compress' :: Eq e => …Run Code Online (Sandbox Code Playgroud)