- 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?
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?