
2 Jun
2011
2 Jun
'11
8:15 a.m.
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