[Haskell-cafe] *GROUP HUG*

Daniel Fischer daniel.is.fischer at googlemail.com
Thu Jun 2 14:15:25 CEST 2011


On Thursday 02 June 2011 01:12:37, Tom Murphy wrote:
> > How about this:
> > 
> > myFoldr :: (a -> b -> b) -> b -> [a] -> b
> > myFoldr f z xs = foldl' (\s x v -> s (x `f` v)) id xs $ z
> > 
> > Cheers,
> > Ivan
> 
> Great! Now I really can say "Come on! It's fun! I can write foldr with
> foldl!"
> 

Unfortunately, you can't, not quite.

A left fold cannont return anything before it has reached the end of the 
list, so it doesn't work on infinite lists.
With a suitable combining function (and suitable input), a right fold can 
start delivering the result early, hence right folds (can) produce results 
for infinite lists (and my example was meant to draw attention to that 
fact).

Cheers,
Daniel



More information about the Haskell-Cafe mailing list