From: simonmar Date: Tue, 28 Sep 2004 09:02:13 +0000 (+0000) Subject: [project @ 2004-09-28 09:02:13 by simonmar] X-Git-Tag: nhc98-1-18-release~244 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=7ae1d0ce9df2c5d7fd5edc8d613061c8c5c14c02;p=ghc-base.git [project @ 2004-09-28 09:02:13 by simonmar] - Move foldl1 to Data.List - Provide a strict version of foldl1, namely foldl1' - Move minimum, maximum to Data.List - Provide specialised versions of minimum & maximum for Int, which use foldl1' --- diff --git a/Data/List.hs b/Data/List.hs index 90fccbe..7d4cf51 100644 --- a/Data/List.hs +++ b/Data/List.hs @@ -454,6 +454,37 @@ insertBy cmp x ys@(y:ys') GT -> y : insertBy cmp x ys' _ -> x : ys +-- | 'maximum' returns the maximum value from a list, +-- which must be non-empty, finite, and of an ordered type. +-- It is a special case of 'Data.List.maximumBy', which allows the +-- programmer to supply their own comparison function. +maximum :: (Ord a) => [a] -> a +maximum [] = errorEmptyList "maximum" +maximum xs = foldl1 max xs + +{-# RULES "maximumInt" maximum = maximumInt #-} + +-- We can't make the overloaded version of maximum strict without +-- changing its semantics (max might not be strict), but we can for +-- the version specialised to 'Int'. +maximumInt :: [Int] -> Int +maximumInt [] = errorEmptyList "maximum" +maximumInt xs = foldl1' max xs + +-- | 'minimum' returns the minimum value from a list, +-- which must be non-empty, finite, and of an ordered type. +-- It is a special case of 'Data.List.minimumBy', which allows the +-- programmer to supply their own comparison function. +minimum :: (Ord a) => [a] -> a +minimum [] = errorEmptyList "minimum" +minimum xs = foldl1 min xs + +{-# RULES "minimumInt" minimum = minimumInt #-} + +minimumInt :: [Int] -> Int +minimumInt [] = errorEmptyList "minimum" +minimumInt xs = foldl1' min xs + -- | The 'maximumBy' function takes a comparison function and a list -- and returns the greatest element of the list by the comparison function. -- The list must be finite and non-empty. @@ -804,6 +835,17 @@ 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 +-- | 'foldl1' is a variant of 'foldl' that has no starting value argument, +-- and thus must be applied to non-empty lists. +foldl1 :: (a -> a -> a) -> [a] -> a +foldl1 f (x:xs) = foldl f x xs +foldl1 _ [] = errorEmptyList "foldl1" + +-- | A strict version of 'foldl1' +foldl1' :: (a -> a -> a) -> [a] -> a +foldl1' f (x:xs) = foldl' f x xs +foldl1' _ [] = errorEmptyList "foldl1'" + #ifdef __GLASGOW_HASKELL__ -- ----------------------------------------------------------------------------- -- List sum and product diff --git a/GHC/List.lhs b/GHC/List.lhs index d431070..edd823e 100644 --- a/GHC/List.lhs +++ b/GHC/List.lhs @@ -19,21 +19,19 @@ module GHC.List ( map, (++), filter, concat, head, last, tail, init, null, length, (!!), - foldl, foldl1, scanl, scanl1, foldr, foldr1, scanr, scanr1, + foldl, scanl, scanl1, foldr, foldr1, scanr, scanr1, iterate, repeat, replicate, cycle, take, drop, splitAt, takeWhile, dropWhile, span, break, reverse, and, or, any, all, elem, notElem, lookup, - maximum, minimum, concatMap, + concatMap, zip, zip3, zipWith, zipWith3, unzip, unzip3, -#ifdef USE_REPORT_PRELUDE - -#else + errorEmptyList, +#ifndef USE_REPORT_PRELUDE -- non-standard, but hidden when creating the Prelude -- export list. takeUInt_append - #endif ) where @@ -168,13 +166,6 @@ foldl f z xs = lgo z xs lgo z [] = z lgo z (x:xs) = lgo (f z x) xs --- | 'foldl1' is a variant of 'foldl' that has no starting value argument, --- and thus must be applied to non-empty lists. - -foldl1 :: (a -> a -> a) -> [a] -> a -foldl1 f (x:xs) = foldl f x xs -foldl1 _ [] = errorEmptyList "foldl1" - -- | 'scanl' is similar to 'foldl', but returns a list of successive -- reduced values from the left: -- @@ -478,25 +469,6 @@ lookup key ((x,y):xys) | key == x = Just y | otherwise = lookup key xys -{-# SPECIALISE maximum :: [Int] -> Int #-} -{-# SPECIALISE minimum :: [Int] -> Int #-} - --- | 'maximum' returns the maximum value from a list, --- which must be non-empty, finite, and of an ordered type. --- It is a special case of 'Data.List.maximumBy', which allows the --- programmer to supply their own comparison function. -maximum :: (Ord a) => [a] -> a -maximum [] = errorEmptyList "maximum" -maximum xs = foldl1 max xs - --- | 'minimum' returns the minimum value from a list, --- which must be non-empty, finite, and of an ordered type. --- It is a special case of 'Data.List.minimumBy', which allows the --- programmer to supply their own comparison function. -minimum :: (Ord a) => [a] -> a -minimum [] = errorEmptyList "minimum" -minimum xs = foldl1 min xs - -- | Map a function over a list and concatenate the results. concatMap :: (a -> [b]) -> [a] -> [b] concatMap f = foldr ((++) . f) []