X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=ghc%2Flib%2Fstd%2FList.lhs;h=4633099db8be49369d025e97c73648afdd93a940;hb=a49a772808296f7d39c079e0e430e80fe94a89c0;hp=4f70d3f0d8eabfae0050c44e49a94adb58f116c8;hpb=f608faec774d5d2cd895240c1e0e66a48aa6cbe3;p=ghc-hetmet.git diff --git a/ghc/lib/std/List.lhs b/ghc/lib/std/List.lhs index 4f70d3f..4633099 100644 --- a/ghc/lib/std/List.lhs +++ b/ghc/lib/std/List.lhs @@ -1,8 +1,10 @@ +% ----------------------------------------------------------------------------- +% $Id: List.lhs,v 1.13 2001/08/29 10:12:34 simonmar 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 @@ -165,13 +167,17 @@ findIndices :: (a -> Bool) -> [a] -> [Int] #ifdef USE_REPORT_PRELUDE findIndices p xs = [ i | (x,i) <- zip xs [0..], p x] #else +#ifdef __HUGS__ +findIndices p xs = [ i | (x,i) <- zip xs [0..], p x] +#else -- Efficient definition findIndices p ls = loop 0# ls where loop _ [] = [] loop n (x:xs) | p x = I# n : loop (n +# 1#) xs | otherwise = loop (n +# 1#) xs -#endif +#endif /* __HUGS__ */ +#endif /* USE_REPORT_PRELUDE */ isPrefixOf :: (Eq a) => [a] -> [a] -> Bool isPrefixOf [] _ = True @@ -203,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 @@ -313,13 +323,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