[project @ 2001-12-21 15:07:20 by simonmar]
[ghc-base.git] / Data / List.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- 
4 -- Module      :  Data.List
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/core/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  provisional
10 -- Portability :  portable
11 --
12 -- $Id: List.hs,v 1.2 2001/12/21 15:07:21 simonmar Exp $
13 --
14 -- Operations on lists.
15 --
16 -----------------------------------------------------------------------------
17
18 module Data.List
19    ( 
20     [] (..),
21
22    , elemIndex         -- :: (Eq a) => a -> [a] -> Maybe Int
23    , elemIndices       -- :: (Eq a) => a -> [a] -> [Int]
24
25    , find              -- :: (a -> Bool) -> [a] -> Maybe a
26    , findIndex         -- :: (a -> Bool) -> [a] -> Maybe Int
27    , findIndices       -- :: (a -> Bool) -> [a] -> [Int]
28    
29    , nub               -- :: (Eq a) => [a] -> [a]
30    , nubBy             -- :: (a -> a -> Bool) -> [a] -> [a]
31
32    , delete            -- :: (Eq a) => a -> [a] -> [a]
33    , deleteBy          -- :: (a -> a -> Bool) -> a -> [a] -> [a]
34    , (\\)              -- :: (Eq a) => [a] -> [a] -> [a]
35    , deleteFirstsBy    -- :: (a -> a -> Bool) -> [a] -> [a] -> [a]
36    
37    , union             -- :: (Eq a) => [a] -> [a] -> [a]
38    , unionBy           -- :: (a -> a -> Bool) -> [a] -> [a] -> [a]
39
40    , intersect         -- :: (Eq a) => [a] -> [a] -> [a]
41    , intersectBy       -- :: (a -> a -> Bool) -> [a] -> [a] -> [a]
42
43    , intersperse       -- :: a -> [a] -> [a]
44    , transpose         -- :: [[a]] -> [[a]]
45    , partition         -- :: (a -> Bool) -> [a] -> ([a], [a])
46
47    , group             -- :: Eq a => [a] -> [[a]]
48    , groupBy           -- :: (a -> a -> Bool) -> [a] -> [[a]]
49
50    , inits             -- :: [a] -> [[a]]
51    , tails             -- :: [a] -> [[a]]
52
53    , isPrefixOf        -- :: (Eq a) => [a] -> [a] -> Bool
54    , isSuffixOf        -- :: (Eq a) => [a] -> [a] -> Bool
55    
56    , mapAccumL         -- :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c])
57    , mapAccumR         -- :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c])
58    
59    , sort              -- :: (Ord a) => [a] -> [a]
60    , sortBy            -- :: (a -> a -> Ordering) -> [a] -> [a]
61    
62    , insert            -- :: (Ord a) => a -> [a] -> [a]
63    , insertBy          -- :: (a -> a -> Ordering) -> a -> [a] -> [a]
64    
65    , maximumBy         -- :: (a -> a -> Ordering) -> [a] -> a
66    , minimumBy         -- :: (a -> a -> Ordering) -> [a] -> a
67    
68    , genericLength     -- :: (Integral a) => [b] -> a
69    , genericTake       -- :: (Integral a) => a -> [b] -> [b]
70    , genericDrop       -- :: (Integral a) => a -> [b] -> [b]
71    , genericSplitAt    -- :: (Integral a) => a -> [b] -> ([b], [b])
72    , genericIndex      -- :: (Integral a) => [b] -> a -> b
73    , genericReplicate  -- :: (Integral a) => a -> b -> [b]
74    
75    , unfoldr            -- :: (b -> Maybe (a, b)) -> b -> [a]
76
77    , zip4, zip5, zip6, zip7
78    , zipWith4, zipWith5, zipWith6, zipWith7
79    , unzip4, unzip5, unzip6, unzip7
80
81    , map               -- :: ( a -> b ) -> [a] -> [b]
82    , (++)              -- :: [a] -> [a] -> [a]
83    , concat            -- :: [[a]] -> [a]
84    , filter            -- :: (a -> Bool) -> [a] -> [a]
85    , head              -- :: [a] -> a
86    , last              -- :: [a] -> a
87    , tail              -- :: [a] -> [a]
88    , init              -- :: [a] -> [a]
89    , null              -- :: [a] -> Bool
90    , length            -- :: [a] -> Int
91    , (!!)              -- :: [a] -> Int -> a
92    , foldl             -- :: (a -> b -> a) -> a -> [b] -> a
93    , foldl1            -- :: (a -> a -> a) -> [a] -> a
94    , scanl             -- :: (a -> b -> a) -> a -> [b] -> [a]
95    , scanl1            -- :: (a -> a -> a) -> [a] -> [a]
96    , foldr             -- :: (a -> b -> b) -> b -> [a] -> b
97    , foldr1            -- :: (a -> a -> a) -> [a] -> a
98    , scanr             -- :: (a -> b -> b) -> b -> [a] -> [b]
99    , scanr1            -- :: (a -> a -> a) -> [a] -> [a]
100    , iterate           -- :: (a -> a) -> a -> [a]
101    , repeat            -- :: a -> [a]
102    , replicate         -- :: Int -> a -> [a]
103    , cycle             -- :: [a] -> [a]
104    , take              -- :: Int -> [a] -> [a]
105    , drop              -- :: Int -> [a] -> [a]
106    , splitAt           -- :: Int -> [a] -> ([a], [a])
107    , takeWhile         -- :: (a -> Bool) -> [a] -> [a]
108    , dropWhile         -- :: (a -> Bool) -> [a] -> [a]
109    , span              -- :: (a -> Bool) -> [a] -> ([a], [a])
110    , break             -- :: (a -> Bool) -> [a] -> ([a], [a])
111
112    , lines             -- :: String   -> [String]
113    , words             -- :: String   -> [String]
114    , unlines           -- :: [String] -> String
115    , unwords           -- :: [String] -> String
116    , reverse           -- :: [a] -> [a]
117    , and               -- :: [Bool] -> Bool
118    , or                -- :: [Bool] -> Bool
119    , any               -- :: (a -> Bool) -> [a] -> Bool
120    , all               -- :: (a -> Bool) -> [a] -> Bool
121    , elem              -- :: a -> [a] -> Bool
122    , notElem           -- :: a -> [a] -> Bool
123    , lookup            -- :: (Eq a) => a -> [(a,b)] -> Maybe b
124    , sum               -- :: (Num a) => [a] -> a
125    , product           -- :: (Num a) => [a] -> a
126    , maximum           -- :: (Ord a) => [a] -> a
127    , minimum           -- :: (Ord a) => [a] -> a
128    , concatMap         -- :: (a -> [b]) -> [a] -> [b]
129    , zip               -- :: [a] -> [b] -> [(a,b)]
130    , zip3  
131    , zipWith           -- :: (a -> b -> c) -> [a] -> [b] -> [c]
132    , zipWith3
133    , unzip             -- :: [(a,b)] -> ([a],[b])
134    , unzip3
135
136    ) where
137
138 import Data.Maybe
139
140 #ifdef __GLASGOW_HASKELL__
141 import GHC.Num
142 import GHC.Real
143 import GHC.List
144 import GHC.Show ( lines, words, unlines, unwords )
145 import GHC.Base
146 #endif
147
148 infix 5 \\ 
149
150 -- -----------------------------------------------------------------------------
151 -- List functions
152
153 elemIndex       :: Eq a => a -> [a] -> Maybe Int
154 elemIndex x     = findIndex (x==)
155
156 elemIndices     :: Eq a => a -> [a] -> [Int]
157 elemIndices x   = findIndices (x==)
158
159 find            :: (a -> Bool) -> [a] -> Maybe a
160 find p          = listToMaybe . filter p
161
162 findIndex       :: (a -> Bool) -> [a] -> Maybe Int
163 findIndex p     = listToMaybe . findIndices p
164
165 findIndices      :: (a -> Bool) -> [a] -> [Int]
166
167 #ifdef USE_REPORT_PRELUDE
168 findIndices p xs = [ i | (x,i) <- zip xs [0..], p x]
169 #else
170 #ifdef __HUGS__
171 findIndices p xs = [ i | (x,i) <- zip xs [0..], p x]
172 #else 
173 -- Efficient definition
174 findIndices p ls = loop 0# ls
175                  where
176                    loop _ [] = []
177                    loop n (x:xs) | p x       = I# n : loop (n +# 1#) xs
178                                  | otherwise = loop (n +# 1#) xs
179 #endif  /* __HUGS__ */
180 #endif  /* USE_REPORT_PRELUDE */
181
182 isPrefixOf              :: (Eq a) => [a] -> [a] -> Bool
183 isPrefixOf [] _         =  True
184 isPrefixOf _  []        =  False
185 isPrefixOf (x:xs) (y:ys)=  x == y && isPrefixOf xs ys
186
187 isSuffixOf              :: (Eq a) => [a] -> [a] -> Bool
188 isSuffixOf x y          =  reverse x `isPrefixOf` reverse y
189
190 -- nub (meaning "essence") remove duplicate elements from its list argument.
191 nub                     :: (Eq a) => [a] -> [a]
192 #ifdef USE_REPORT_PRELUDE
193 nub                     =  nubBy (==)
194 #else
195 -- stolen from HBC
196 nub l                   = nub' l []             -- '
197   where
198     nub' [] _           = []                    -- '
199     nub' (x:xs) ls                              -- '
200         | x `elem` ls   = nub' xs ls            -- '
201         | otherwise     = x : nub' xs (x:ls)    -- '
202 #endif
203
204 nubBy                   :: (a -> a -> Bool) -> [a] -> [a]
205 #ifdef USE_REPORT_PRELUDE
206 nubBy eq []             =  []
207 nubBy eq (x:xs)         =  x : nubBy eq (filter (\ y -> not (eq x y)) xs)
208 #else
209 nubBy eq l              = nubBy' l []
210   where
211     nubBy' [] _         = []
212     nubBy' (y:ys) xs
213        | elem_by eq y xs = nubBy' ys xs 
214        | otherwise       = y : nubBy' ys (y:xs)
215
216 -- Not exported:
217 -- Note that we keep the call to `eq` with arguments in the
218 -- same order as in the reference implementation
219 -- 'xs' is the list of things we've seen so far, 
220 -- 'y' is the potential new element
221 elem_by :: (a -> a -> Bool) -> a -> [a] -> Bool
222 elem_by _  _ []         =  False
223 elem_by eq y (x:xs)     =  x `eq` y || elem_by eq y xs
224 #endif
225
226
227 -- delete x removes the first occurrence of x from its list argument.
228 delete                  :: (Eq a) => a -> [a] -> [a]
229 delete                  =  deleteBy (==)
230
231 deleteBy                :: (a -> a -> Bool) -> a -> [a] -> [a]
232 deleteBy _  _ []        = []
233 deleteBy eq x (y:ys)    = if x `eq` y then ys else y : deleteBy eq x ys
234
235 -- list difference (non-associative).  In the result of xs \\ ys,
236 -- the first occurrence of each element of ys in turn (if any)
237 -- has been removed from xs.  Thus, (xs ++ ys) \\ xs == ys.
238 (\\)                    :: (Eq a) => [a] -> [a] -> [a]
239 (\\)                    =  foldl (flip delete)
240
241 -- List union, remove the elements of first list from second.
242 union                   :: (Eq a) => [a] -> [a] -> [a]
243 union                   = unionBy (==)
244
245 unionBy                 :: (a -> a -> Bool) -> [a] -> [a] -> [a]
246 unionBy eq xs ys        =  xs ++ foldl (flip (deleteBy eq)) (nubBy eq ys) xs
247
248 intersect               :: (Eq a) => [a] -> [a] -> [a]
249 intersect               =  intersectBy (==)
250
251 intersectBy             :: (a -> a -> Bool) -> [a] -> [a] -> [a]
252 intersectBy eq xs ys    =  [x | x <- xs, any (eq x) ys]
253
254 -- intersperse sep inserts sep between the elements of its list argument.
255 -- e.g. intersperse ',' "abcde" == "a,b,c,d,e"
256 intersperse             :: a -> [a] -> [a]
257 intersperse _   []      = []
258 intersperse _   [x]     = [x]
259 intersperse sep (x:xs)  = x : sep : intersperse sep xs
260
261 transpose               :: [[a]] -> [[a]]
262 transpose []             = []
263 transpose ([]   : xss)   = transpose xss
264 transpose ((x:xs) : xss) = (x : [h | (h:t) <- xss]) : transpose (xs : [ t | (h:t) <- xss])
265
266
267 -- partition takes a predicate and a list and returns a pair of lists:
268 -- those elements of the argument list that do and do not satisfy the
269 -- predicate, respectively; i,e,,
270 -- partition p xs == (filter p xs, filter (not . p) xs).
271 partition               :: (a -> Bool) -> [a] -> ([a],[a])
272 {-# INLINE partition #-}
273 partition p xs = foldr (select p) ([],[]) xs
274
275 select p x (ts,fs) | p x       = (x:ts,fs)
276                    | otherwise = (ts, x:fs)
277
278 -- @mapAccumL@ behaves like a combination
279 -- of  @map@ and @foldl@;
280 -- it applies a function to each element of a list, passing an accumulating
281 -- parameter from left to right, and returning a final value of this
282 -- accumulator together with the new list.
283
284 mapAccumL :: (acc -> x -> (acc, y)) -- Function of elt of input list
285                                     -- and accumulator, returning new
286                                     -- accumulator and elt of result list
287           -> acc            -- Initial accumulator 
288           -> [x]            -- Input list
289           -> (acc, [y])     -- Final accumulator and result list
290 mapAccumL _ s []        =  (s, [])
291 mapAccumL f s (x:xs)    =  (s'',y:ys)
292                            where (s', y ) = f s x
293                                  (s'',ys) = mapAccumL f s' xs
294
295 -- @mapAccumR@ does the same, but working from right to left instead.
296 -- Its type is the same as @mapAccumL@, though.
297
298 mapAccumR :: (acc -> x -> (acc, y))     -- Function of elt of input list
299                                         -- and accumulator, returning new
300                                         -- accumulator and elt of result list
301             -> acc              -- Initial accumulator
302             -> [x]              -- Input list
303             -> (acc, [y])               -- Final accumulator and result list
304 mapAccumR _ s []        =  (s, [])
305 mapAccumR f s (x:xs)    =  (s'', y:ys)
306                            where (s'',y ) = f s' x
307                                  (s', ys) = mapAccumR f s xs
308
309
310 insert :: Ord a => a -> [a] -> [a]
311 insert e ls = insertBy (compare) e ls
312
313 insertBy :: (a -> a -> Ordering) -> a -> [a] -> [a]
314 insertBy _   x [] = [x]
315 insertBy cmp x ys@(y:ys')
316  = case cmp x y of
317      GT -> y : insertBy cmp x ys'
318      _  -> x : ys
319
320 maximumBy               :: (a -> a -> Ordering) -> [a] -> a
321 maximumBy _ []          =  error "List.maximumBy: empty list"
322 maximumBy cmp xs        =  foldl1 max xs
323                         where
324                            max x y = case cmp x y of
325                                         GT -> x
326                                         _  -> y
327
328 minimumBy               :: (a -> a -> Ordering) -> [a] -> a
329 minimumBy _ []          =  error "List.minimumBy: empty list"
330 minimumBy cmp xs        =  foldl1 min xs
331                         where
332                            min x y = case cmp x y of
333                                         GT -> y
334                                         _  -> x
335
336 genericLength           :: (Num i) => [b] -> i
337 genericLength []        =  0
338 genericLength (_:l)     =  1 + genericLength l
339
340 genericTake             :: (Integral i) => i -> [a] -> [a]
341 genericTake 0 _         =  []
342 genericTake _ []        =  []
343 genericTake n (x:xs) | n > 0  =  x : genericTake (n-1) xs
344 genericTake _  _        =  error "List.genericTake: negative argument"
345
346 genericDrop             :: (Integral i) => i -> [a] -> [a]
347 genericDrop 0 xs        =  xs
348 genericDrop _ []        =  []
349 genericDrop n (_:xs) | n > 0  =  genericDrop (n-1) xs
350 genericDrop _ _         =  error "List.genericDrop: negative argument"
351
352 genericSplitAt          :: (Integral i) => i -> [b] -> ([b],[b])
353 genericSplitAt 0 xs     =  ([],xs)
354 genericSplitAt _ []     =  ([],[])
355 genericSplitAt n (x:xs) | n > 0  =  (x:xs',xs'') where
356                                (xs',xs'') = genericSplitAt (n-1) xs
357 genericSplitAt _ _      =  error "List.genericSplitAt: negative argument"
358
359
360 genericIndex :: (Integral a) => [b] -> a -> b
361 genericIndex (x:_)  0 = x
362 genericIndex (_:xs) n 
363  | n > 0     = genericIndex xs (n-1)
364  | otherwise = error "List.genericIndex: negative argument."
365 genericIndex _ _      = error "List.genericIndex: index too large."
366
367 genericReplicate        :: (Integral i) => i -> a -> [a]
368 genericReplicate n x    =  genericTake n (repeat x)
369
370
371 zip4                    :: [a] -> [b] -> [c] -> [d] -> [(a,b,c,d)]
372 zip4                    =  zipWith4 (,,,)
373
374 zip5                    :: [a] -> [b] -> [c] -> [d] -> [e] -> [(a,b,c,d,e)]
375 zip5                    =  zipWith5 (,,,,)
376
377 zip6                    :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> 
378                               [(a,b,c,d,e,f)]
379 zip6                    =  zipWith6 (,,,,,)
380
381 zip7                    :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] ->
382                               [g] -> [(a,b,c,d,e,f,g)]
383 zip7                    =  zipWith7 (,,,,,,)
384
385 zipWith4                :: (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
386 zipWith4 z (a:as) (b:bs) (c:cs) (d:ds)
387                         =  z a b c d : zipWith4 z as bs cs ds
388 zipWith4 _ _ _ _ _      =  []
389
390 zipWith5                :: (a->b->c->d->e->f) -> 
391                            [a]->[b]->[c]->[d]->[e]->[f]
392 zipWith5 z (a:as) (b:bs) (c:cs) (d:ds) (e:es)
393                         =  z a b c d e : zipWith5 z as bs cs ds es
394 zipWith5 _ _ _ _ _ _    = []
395
396 zipWith6                :: (a->b->c->d->e->f->g) ->
397                            [a]->[b]->[c]->[d]->[e]->[f]->[g]
398 zipWith6 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs)
399                         =  z a b c d e f : zipWith6 z as bs cs ds es fs
400 zipWith6 _ _ _ _ _ _ _  = []
401
402 zipWith7                :: (a->b->c->d->e->f->g->h) ->
403                            [a]->[b]->[c]->[d]->[e]->[f]->[g]->[h]
404 zipWith7 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs) (g:gs)
405                    =  z a b c d e f g : zipWith7 z as bs cs ds es fs gs
406 zipWith7 _ _ _ _ _ _ _ _ = []
407
408 unzip4                  :: [(a,b,c,d)] -> ([a],[b],[c],[d])
409 unzip4                  =  foldr (\(a,b,c,d) ~(as,bs,cs,ds) ->
410                                         (a:as,b:bs,c:cs,d:ds))
411                                  ([],[],[],[])
412
413 unzip5                  :: [(a,b,c,d,e)] -> ([a],[b],[c],[d],[e])
414 unzip5                  =  foldr (\(a,b,c,d,e) ~(as,bs,cs,ds,es) ->
415                                         (a:as,b:bs,c:cs,d:ds,e:es))
416                                  ([],[],[],[],[])
417
418 unzip6                  :: [(a,b,c,d,e,f)] -> ([a],[b],[c],[d],[e],[f])
419 unzip6                  =  foldr (\(a,b,c,d,e,f) ~(as,bs,cs,ds,es,fs) ->
420                                         (a:as,b:bs,c:cs,d:ds,e:es,f:fs))
421                                  ([],[],[],[],[],[])
422
423 unzip7          :: [(a,b,c,d,e,f,g)] -> ([a],[b],[c],[d],[e],[f],[g])
424 unzip7          =  foldr (\(a,b,c,d,e,f,g) ~(as,bs,cs,ds,es,fs,gs) ->
425                                 (a:as,b:bs,c:cs,d:ds,e:es,f:fs,g:gs))
426                          ([],[],[],[],[],[],[])
427
428
429
430 deleteFirstsBy          :: (a -> a -> Bool) -> [a] -> [a] -> [a]
431 deleteFirstsBy eq       =  foldl (flip (deleteBy eq))
432
433
434 -- group splits its list argument into a list of lists of equal, adjacent
435 -- elements.  e.g.,
436 -- group "Mississippi" == ["M","i","ss","i","ss","i","pp","i"]
437 group                   :: (Eq a) => [a] -> [[a]]
438 group                   =  groupBy (==)
439
440 groupBy                 :: (a -> a -> Bool) -> [a] -> [[a]]
441 groupBy _  []           =  []
442 groupBy eq (x:xs)       =  (x:ys) : groupBy eq zs
443                            where (ys,zs) = span (eq x) xs
444
445 -- inits xs returns the list of initial segments of xs, shortest first.
446 -- e.g., inits "abc" == ["","a","ab","abc"]
447 inits                   :: [a] -> [[a]]
448 inits []                =  [[]]
449 inits (x:xs)            =  [[]] ++ map (x:) (inits xs)
450
451 -- tails xs returns the list of all final segments of xs, longest first.
452 -- e.g., tails "abc" == ["abc", "bc", "c",""]
453 tails                   :: [a] -> [[a]]
454 tails []                =  [[]]
455 tails xxs@(_:xs)        =  xxs : tails xs
456
457
458 ------------------------------------------------------------------------------
459 -- Quick Sort algorithm taken from HBC's QSort library.
460
461 sort :: (Ord a) => [a] -> [a]
462 sortBy :: (a -> a -> Ordering) -> [a] -> [a]
463
464 #ifdef USE_REPORT_PRELUDE
465 sort = sortBy compare
466 sortBy cmp = foldr (insertBy cmp) []
467 #else
468
469 sortBy cmp l = qsort cmp l []
470 sort l = qsort compare l []
471
472 -- rest is not exported:
473
474 -- qsort is stable and does not concatenate.
475 qsort :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
476 qsort _   []     r = r
477 qsort _   [x]    r = x:r
478 qsort cmp (x:xs) r = qpart cmp x xs [] [] r
479
480 -- qpart partitions and sorts the sublists
481 qpart :: (a -> a -> Ordering) -> a -> [a] -> [a] -> [a] -> [a] -> [a]
482 qpart cmp x [] rlt rge r =
483     -- rlt and rge are in reverse order and must be sorted with an
484     -- anti-stable sorting
485     rqsort cmp rlt (x:rqsort cmp rge r)
486 qpart cmp x (y:ys) rlt rge r =
487     case cmp x y of
488         GT -> qpart cmp x ys (y:rlt) rge r
489         _  -> qpart cmp x ys rlt (y:rge) r
490
491 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
492 rqsort :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
493 rqsort _   []     r = r
494 rqsort _   [x]    r = x:r
495 rqsort cmp (x:xs) r = rqpart cmp x xs [] [] r
496
497 rqpart :: (a -> a -> Ordering) -> a -> [a] -> [a] -> [a] -> [a] -> [a]
498 rqpart cmp x [] rle rgt r =
499     qsort cmp rle (x:qsort cmp rgt r)
500 rqpart cmp x (y:ys) rle rgt r =
501     case cmp y x of
502         GT -> rqpart cmp x ys rle (y:rgt) r
503         _  -> rqpart cmp x ys (y:rle) rgt r
504
505 #endif /* USE_REPORT_PRELUDE */
506
507 {-
508 \begin{verbatim}
509   unfoldr f' (foldr f z xs) == (z,xs)
510
511  if the following holds:
512
513    f' (f x y) = Just (x,y)
514    f' z       = Nothing
515 \end{verbatim}
516 -}
517
518 unfoldr      :: (b -> Maybe (a, b)) -> b -> [a]
519 unfoldr f b  =
520   case f b of
521    Just (a,new_b) -> a : unfoldr f new_b
522    Nothing        -> []
523
524 -- -----------------------------------------------------------------------------
525 -- List sum and product
526
527 -- sum and product compute the sum or product of a finite list of numbers.
528 {-# SPECIALISE sum     :: [Int] -> Int #-}
529 {-# SPECIALISE sum     :: [Integer] -> Integer #-}
530 {-# SPECIALISE product :: [Int] -> Int #-}
531 {-# SPECIALISE product :: [Integer] -> Integer #-}
532 sum, product            :: (Num a) => [a] -> a
533 #ifdef USE_REPORT_PRELUDE
534 sum                     =  foldl (+) 0  
535 product                 =  foldl (*) 1
536 #else
537 sum     l       = sum' l 0
538   where
539     sum' []     a = a
540     sum' (x:xs) a = sum' xs (a+x)
541 product l       = prod l 1
542   where
543     prod []     a = a
544     prod (x:xs) a = prod xs (a*x)
545 #endif