From 8d0e6c63640414fda69fe77c126f10128a90a5f3 Mon Sep 17 00:00:00 2001 From: simonpj Date: Mon, 26 Feb 2001 09:29:32 +0000 Subject: [PATCH] [project @ 2001-02-26 09:29:32 by simonpj] Make foldl more efficient; see comments with foldl --- ghc/lib/std/PrelList.lhs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ghc/lib/std/PrelList.lhs b/ghc/lib/std/PrelList.lhs index 27d0d4f..7453388 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 -- 1.7.10.4