相关疑难解决方法(0)

使用foldr编写foldl

Real World Haskell中,第4章.函数式编程

用foldr写foldl:

-- file: ch04/Fold.hs
myFoldl :: (a -> b -> a) -> a -> [b] -> a

myFoldl f z xs = foldr step id xs z
    where step x g a = g (f a x)
Run Code Online (Sandbox Code Playgroud)

上面的代码让我很困惑,有人打电话给dps用一个有意义的名字重写它,使它更清晰:

myFoldl stepL zeroL xs = (foldr stepR id xs) zeroL
where stepR lastL accR accInitL = accR (stepL accInitL lastL)
Run Code Online (Sandbox Code Playgroud)

其他人,Jef G,通过提供一个例子并逐步展示基础机制,做得非常出色:

myFoldl (+) 0 [1, 2, 3]
= (foldR step id [1, 2, …
Run Code Online (Sandbox Code Playgroud)

recursion haskell fold

73
推荐指数
4
解决办法
1万
查看次数

标签 统计

fold ×1

haskell ×1

recursion ×1