Fork me on GitHub

哪位大牛解释一下Real World Haskell第95页的myFoldl函数是怎么回事呗。。。想了好久,写写画画了好久,理解不了啊。

Real World Haskell第95页
In fact, we can even write foldl using foldr!
--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)

Submitted by at 11 months ago

RealWorldHaskellmyFoldlfoldlfoldrid

所有回复

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)
可知:
f :: a->b->a
z :: a
xs :: [a]
x :: a
g :: a->b
step :: a->(b->c)->b->c

foldr :: (a->b->b)->b->[a]->b
foldr :: (a->(b->c)->(b->c))->(b->c)->[a]->(b->c)

btnz 11 months ago