foldr in Haskell

toughwimp11

Senior member
May 8, 2005
415
0
76
Does anyone know why foldr in Haskell is not defined in a tail-recursive manner? It seems like it's be more efficient to have a foldr sort of like this:

foldr0 f init [] [] = init
foldr0 f init (x:xs) [] = foldr0 f (f x init) xs []
foldr0 f init xs (y:ys) = foldr0 f init (y:xs) ys

foldr f init xs = foldr0 f init [] xs

rather than:

foldr f z [] = z
foldr f z (x:xs) = f x (foldr f z xs)

although the code is a bit more complicated I guess. Any ideas?
 

dinkumthinkum

Senior member
Jul 3, 2008
203
0
0
What you are doing is effectively reversing the list first, before folding.

Remember that
Code:
foldr op init xs
is supposed to produce the equivalent of
Code:
  x1 op (x2 op (x3 op (x4 op ... (xn op z) ... )))

and that Haskell is a lazy language that can handle infinite data structures.

So the simple demo which shows that your function is not equivalent to the real foldr is this:
Code:
take 10 $ foldr (:) [] [1..]
the real foldr outputs a list from 1 to 10. Your foldr goes on an infinite loop as it attempts to reverse the infinite list 1 to infinity.

In general, here's a tip: use foldr most of the time unless you're absolutely sure you will not gain any advantage from laziness (partially computed folds). If you want a strict fold, use foldl' from the Data.List package. That is tail-recursive, and it forces every iteration as well.

For example, you don't gain anything from partially computed summation of numbers (except a waste of memory as the delayed computation is aggregated), so to sum up a list of numbers you should do
Code:
sum' ns = foldl' (+) 0 ns
it is a long-standing and well-known problem that the Haskell report defines the sum function with foldl, but this gets the worst of both laziness and strictness. Use sum' as defined above instead.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |