[project @ 2001-08-04 06:19:54 by ken]
[ghc-hetmet.git] / ghc / lib / std / List.lhs
index 08c2ddf..93640a4 100644 (file)
@@ -1,8 +1,10 @@
+% -----------------------------------------------------------------------------
+% $Id: List.lhs,v 1.12 2001/06/25 13:13:58 simonpj Exp $
 %
-% (c) The AQUA Project, Glasgow University, 1994-1999
+% (c) The University of Glasgow, 1994-2000
 %
 
-\section[List]{Module @Lhar@}
+\section[List]{Module @List@}
 
 \begin{code}
 module List 
@@ -65,7 +67,7 @@ module List
    , genericIndex      -- :: (Integral a) => [b] -> a -> b
    , genericReplicate  -- :: (Integral a) => a -> b -> [b]
    
-   , unfoldr           -- :: (a -> Maybe (b,a)) -> a -> (a,[b])
+   , unfoldr           -- :: (b -> Maybe (a, b)) -> b -> [a]
 
    , zip4, zip5, zip6, zip7
    , zipWith4, zipWith5, zipWith6, zipWith7
@@ -207,14 +209,18 @@ nubBy eq (x:xs)         =  x : nubBy eq (filter (\ y -> not (eq x y)) xs)
 nubBy eq l              = nubBy' l []
   where
     nubBy' [] _                = []
-    nubBy' (x:xs) ls
-       | elemBy eq x ls = nubBy' xs ls 
-       | otherwise     = x : nubBy' xs (x:ls)
-
---not exported:
-elemBy :: (a -> a -> Bool) -> a -> [a] -> Bool
-elemBy _  _ []         =  False
-elemBy eq x (y:ys)     =  x `eq` y || elemBy eq x ys
+    nubBy' (y:ys) xs
+       | elem_by eq y xs = nubBy' ys xs 
+       | otherwise      = y : nubBy' ys (y:xs)
+
+-- Not exported:
+-- Note that we keep the call to `eq` with arguments in the
+-- same order as in the reference implementation
+-- 'xs' is the list of things we've seen so far, 
+-- 'y' is the potential new element
+elem_by :: (a -> a -> Bool) -> a -> [a] -> Bool
+elem_by _  _ []                =  False
+elem_by eq y (x:xs)    =  x `eq` y || elem_by eq y xs
 #endif
 
 
@@ -266,8 +272,8 @@ partition           :: (a -> Bool) -> [a] -> ([a],[a])
 {-# INLINE partition #-}
 partition p xs = foldr (select p) ([],[]) xs
 
-select p x ~(ts,fs) | p x       = (x:ts,fs)
-                    | otherwise = (ts, x:fs)
+select p x (ts,fs) | p x       = (x:ts,fs)
+                   | otherwise = (ts, x:fs)
 \end{code}
 
 @mapAccumL@ behaves like a combination
@@ -515,35 +521,3 @@ unfoldr f b  =
    Just (a,new_b) -> a : unfoldr f new_b
    Nothing        -> []
 \end{code}
-
-#if 0  /* should go in PrelList, but dependency problems */
-foldl' is a strict version of foldl; that is, it doesn't build up a
-huge suspension in its first argument as it traverses the list.  Valid
-when f is strict.
-
-\begin{code}
-foldl'                   :: (a -> b -> a) -> a -> [b] -> a
-foldl' _ z []            =  z
-foldl' f z (x:xs)        =  let a = f z x in seq a (foldl f a xs)
-
-foldl1'                         :: (a -> a -> a) -> [a] -> a
-foldl1'        f (x:xs)         =  foldl' f x xs
-foldl1'        _ []             =  errorEmptyList "foldl1'"
-
-{-# RULES
-"maximumInt"    maximum = maximum' :: [Int]     -> Int
-"maximumInteger" maximum = maximum' :: [Integer] -> Integer
-"minimumInt"     minimum = minimum' :: [Int]     -> Int
-"minimumInteger" minimum = minimum' :: [Integer] -> Integer
- #-}
-
-{-# SPECIALISE  maximum' :: [Int] -> Int #-}
-{-# SPECIALISE  minimum' :: [Int] -> Int #-}
-
-maximum' []             =  errorEmptyList "maximum'"
-maximum' xs             =  foldl1' max xs
-
-minimum' []             =  errorEmptyList "minimum'"
-minimum' xs             =  foldl1' min xs
-\end{code}
-#endif