Haskell newb在这里
我正在研究haskell中的这个问题:
(**) 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.
Example:
* (compress '(a a a a b c c a a d e e e e))
(A B C A D E)
Run Code Online (Sandbox Code Playgroud)
解决方案(我必须查找)使用foldr:
compress' :: (Eq a) => [a] -> [a]
compress' xs = foldr (\x acc -> if x == (head acc) …Run Code Online (Sandbox Code Playgroud)