X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FList.hs;h=da6c878a49b7990dc8eb82ea6c9720febbaead83;hb=dd22d02f42cf007d5bf8be78ade260b19efec12f;hp=5b8d7672d01ed6b6b44a864edbb93b28e8c3e0e4;hpb=6f7021de17af64835e2dc00f13321f3ec691b5a0;p=haskell-directory.git diff --git a/Data/List.hs b/Data/List.hs index 5b8d767..da6c878 100644 --- a/Data/List.hs +++ b/Data/List.hs @@ -1,4 +1,4 @@ -{-# OPTIONS -fno-implicit-prelude #-} +{-# OPTIONS_GHC -fno-implicit-prelude #-} ----------------------------------------------------------------------------- -- | -- Module : Data.List @@ -42,6 +42,7 @@ module Data.List , foldl -- :: (a -> b -> a) -> a -> [b] -> a , foldl' -- :: (a -> b -> a) -> a -> [b] -> a , foldl1 -- :: (a -> a -> a) -> [a] -> a + , foldl1' -- :: (a -> a -> a) -> [a] -> a , foldr -- :: (a -> b -> b) -> b -> [a] -> b , foldr1 -- :: (a -> a -> a) -> [a] -> a @@ -209,7 +210,7 @@ import GHC.List import GHC.Base #endif -infix 5 \\ +infix 5 \\ -- comment to fool cpp -- ----------------------------------------------------------------------------- -- List functions @@ -454,6 +455,47 @@ insertBy cmp x ys@(y:ys') GT -> y : insertBy cmp x ys' _ -> x : ys +#ifdef __GLASGOW_HASKELL__ + +-- | '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 = (strictMaximum :: [Int] -> Int); + "maximumInteger" maximum = (strictMaximum :: [Integer] -> Integer) + #-} + +-- 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'. +strictMaximum :: (Ord a) => [a] -> a +strictMaximum [] = errorEmptyList "maximum" +strictMaximum 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 = (strictMinimum :: [Int] -> Int); + "minimumInteger" minimum = (strictMinimum :: [Integer] -> Integer) + #-} + +strictMinimum :: (Ord a) => [a] -> a +strictMinimum [] = errorEmptyList "minimum" +strictMinimum xs = foldl1' min xs + +#endif /* __GLASGOW_HASKELL__ */ + -- | 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. @@ -805,6 +847,19 @@ foldl' f a [] = a foldl' f a (x:xs) = let a' = f a x in a' `seq` foldl' f a' xs #ifdef __GLASGOW_HASKELL__ +-- | '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" +#endif /* __GLASGOW_HASKELL__ */ + +-- | 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 @@ -876,4 +931,11 @@ unwords [] = "" unwords [w] = w unwords (w:ws) = w ++ ' ' : unwords ws #endif -#endif /* __GLASGOW_HASKELL__ */ + +#else /* !__GLASGOW_HASKELL__ */ + +errorEmptyList :: String -> a +errorEmptyList fun = + error ("Prelude." ++ fun ++ ": empty list") + +#endif /* !__GLASGOW_HASKELL__ */