[Haskell-cafe] A systematic method for deriving a defintion of foldl using foldr?

Henning Thielemann lemming at henning-thielemann.de
Fri Mar 13 21:56:07 EDT 2009


On Wed, 11 Mar 2009, R J wrote:

> foldl and foldr are defined as follows:
> 
>   foldr                :: (a -> b -> b) -> b -> [a] -> b
>   foldr f e []         =  e
>   foldr f e (x : xs)   =  f x (foldr f e xs)
> 
>   foldl                :: (b -> a -> b) -> b -> [a] -> b
>   foldl f e []         =  e
>   foldl f e (x : xs)   =  foldl f (f e x) xs
> 
> 1.  I understand how these definitions work, and yet I'm unable to implement foldl in terms
> of foldr.  What's a systematic approach to identifying such an implementation, and what is
> the implementation?

You are lucky, I recently wrote
   http://haskell.org/haskellwiki/Foldl_as_foldr


More information about the Haskell-Cafe mailing list