[project @ 2002-05-09 13:16:29 by simonmar]
[ghc-base.git] / Data / List.hs
index ce4c9b3..245712f 100644 (file)
@@ -1,16 +1,14 @@
 {-# OPTIONS -fno-implicit-prelude #-}
 -----------------------------------------------------------------------------
--- 
+-- |
 -- Module      :  Data.List
 -- Copyright   :  (c) The University of Glasgow 2001
--- License     :  BSD-style (see the file libraries/core/LICENSE)
+-- License     :  BSD-style (see the file libraries/base/LICENSE)
 -- 
 -- Maintainer  :  libraries@haskell.org
 -- Stability   :  provisional
 -- Portability :  portable
 --
--- $Id: List.hs,v 1.1 2001/06/28 14:15:02 simonmar Exp $
---
 -- Operations on lists.
 --
 -----------------------------------------------------------------------------
@@ -90,6 +88,7 @@ module Data.List
    , length           -- :: [a] -> Int
    , (!!)             -- :: [a] -> Int -> a
    , foldl            -- :: (a -> b -> a) -> a -> [b] -> a
+   , foldl'           -- :: (a -> b -> a) -> a -> [b] -> a
    , foldl1           -- :: (a -> a -> a) -> [a] -> a
    , scanl             -- :: (a -> b -> a) -> a -> [b] -> [a]
    , scanl1            -- :: (a -> a -> a) -> [a] -> [a]
@@ -317,13 +316,21 @@ insertBy cmp x ys@(y:ys')
      GT -> y : insertBy cmp x ys'
      _  -> x : ys
 
-maximumBy              :: (a -> a -> a) -> [a] -> a
-maximumBy _   []       =  error "List.maximumBy: empty list"
-maximumBy max xs       =  foldl1 max xs
-
-minimumBy              :: (a -> a -> a) -> [a] -> a
-minimumBy _   []       =  error "List.minimumBy: empty list"
-minimumBy min xs       =  foldl1 min xs
+maximumBy              :: (a -> a -> Ordering) -> [a] -> a
+maximumBy _ []         =  error "List.maximumBy: empty list"
+maximumBy cmp xs       =  foldl1 max xs
+                       where
+                          max x y = case cmp x y of
+                                       GT -> x
+                                       _  -> y
+
+minimumBy              :: (a -> a -> Ordering) -> [a] -> a
+minimumBy _ []         =  error "List.minimumBy: empty list"
+minimumBy cmp xs       =  foldl1 min xs
+                       where
+                          min x y = case cmp x y of
+                                       GT -> y
+                                       _  -> x
 
 genericLength           :: (Num i) => [b] -> i
 genericLength []        =  0
@@ -513,6 +520,14 @@ unfoldr f b  =
    Just (a,new_b) -> a : unfoldr f new_b
    Nothing        -> []
 
+
+-- -----------------------------------------------------------------------------
+-- strict version of foldl
+
+foldl'           :: (a -> b -> a) -> a -> [b] -> a
+foldl' f a []     = a
+foldl' f a (x:xs) = let a' = f a x in a' `seq` foldl' f a' xs
+
 -- -----------------------------------------------------------------------------
 -- List sum and product