summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsimonpj <unknown>2001-02-26 09:29:32 +0000
committersimonpj <unknown>2001-02-26 09:29:32 +0000
commit8d0e6c63640414fda69fe77c126f10128a90a5f3 (patch)
treea282f3a43374ba96ec36539de84c9683108747d5
parent5792b355352b5e2112cffdbbd413ead8b6be7bdf (diff)
downloadhaskell-8d0e6c63640414fda69fe77c126f10128a90a5f3.tar.gz
[project @ 2001-02-26 09:29:32 by simonpj]
Make foldl more efficient; see comments with foldl
-rw-r--r--ghc/lib/std/PrelList.lhs14
1 files changed, 10 insertions, 4 deletions
diff --git a/ghc/lib/std/PrelList.lhs b/ghc/lib/std/PrelList.lhs
index 27d0d4f83d..74533887a2 100644
--- a/ghc/lib/std/PrelList.lhs
+++ b/ghc/lib/std/PrelList.lhs
@@ -1,5 +1,5 @@
% ------------------------------------------------------------------------------
-% $Id: PrelList.lhs,v 1.22 2000/09/14 13:46:42 simonpj Exp $
+% $Id: PrelList.lhs,v 1.23 2001/02/26 09:29:32 simonpj Exp $
%
% (c) The University of Glasgow, 1994-2000
%
@@ -157,9 +157,15 @@ filterList pred (x:xs)
-- scanl1 is similar, again without the starting element:
-- scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
-foldl :: (a -> b -> a) -> a -> [b] -> a
-foldl _ z [] = z
-foldl f z (x:xs) = foldl f (f z x) xs
+-- We write foldl as a non-recursive thing, so that it
+-- can be inlined, and then (often) strictness-analysed,
+-- and hence the classic space leak on foldl (+) 0 xs
+
+foldl :: (a -> b -> a) -> a -> [b] -> a
+foldl f z xs = lgo z xs
+ where
+ lgo z [] = z
+ lgo z (x:xs) = lgo (f z x) xs
foldl1 :: (a -> a -> a) -> [a] -> a
foldl1 f (x:xs) = foldl f x xs