小编Pav*_*vel的帖子

Haskell奇怪(对我而言)行为

我正在研究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)

haskell computation

1
推荐指数
1
解决办法
75
查看次数

标签 统计

computation ×1

haskell ×1