1 % -----------------------------------------------------------------------------
2 % $Id: List.lhs,v 1.11 2000/08/18 06:44:05 qrczak Exp $
4 % (c) The University of Glasgow, 1994-2000
7 \section[List]{Module @List@}
17 elemIndex -- :: (Eq a) => a -> [a] -> Maybe Int
18 , elemIndices -- :: (Eq a) => a -> [a] -> [Int]
20 , find -- :: (a -> Bool) -> [a] -> Maybe a
21 , findIndex -- :: (a -> Bool) -> [a] -> Maybe Int
22 , findIndices -- :: (a -> Bool) -> [a] -> [Int]
24 , nub -- :: (Eq a) => [a] -> [a]
25 , nubBy -- :: (a -> a -> Bool) -> [a] -> [a]
27 , delete -- :: (Eq a) => a -> [a] -> [a]
28 , deleteBy -- :: (a -> a -> Bool) -> a -> [a] -> [a]
29 , (\\) -- :: (Eq a) => [a] -> [a] -> [a]
30 , deleteFirstsBy -- :: (a -> a -> Bool) -> [a] -> [a] -> [a]
32 , union -- :: (Eq a) => [a] -> [a] -> [a]
33 , unionBy -- :: (a -> a -> Bool) -> [a] -> [a] -> [a]
35 , intersect -- :: (Eq a) => [a] -> [a] -> [a]
36 , intersectBy -- :: (a -> a -> Bool) -> [a] -> [a] -> [a]
38 , intersperse -- :: a -> [a] -> [a]
39 , transpose -- :: [[a]] -> [[a]]
40 , partition -- :: (a -> Bool) -> [a] -> ([a], [a])
42 , group -- :: Eq a => [a] -> [[a]]
43 , groupBy -- :: (a -> a -> Bool) -> [a] -> [[a]]
45 , inits -- :: [a] -> [[a]]
46 , tails -- :: [a] -> [[a]]
48 , isPrefixOf -- :: (Eq a) => [a] -> [a] -> Bool
49 , isSuffixOf -- :: (Eq a) => [a] -> [a] -> Bool
51 , mapAccumL -- :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c])
52 , mapAccumR -- :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c])
54 , sort -- :: (Ord a) => [a] -> [a]
55 , sortBy -- :: (a -> a -> Ordering) -> [a] -> [a]
57 , insert -- :: (Ord a) => a -> [a] -> [a]
58 , insertBy -- :: (a -> a -> Ordering) -> a -> [a] -> [a]
60 , maximumBy -- :: (a -> a -> Ordering) -> [a] -> a
61 , minimumBy -- :: (a -> a -> Ordering) -> [a] -> a
63 , genericLength -- :: (Integral a) => [b] -> a
64 , genericTake -- :: (Integral a) => a -> [b] -> [b]
65 , genericDrop -- :: (Integral a) => a -> [b] -> [b]
66 , genericSplitAt -- :: (Integral a) => a -> [b] -> ([b], [b])
67 , genericIndex -- :: (Integral a) => [b] -> a -> b
68 , genericReplicate -- :: (Integral a) => a -> b -> [b]
70 , unfoldr -- :: (b -> Maybe (a, b)) -> b -> [a]
72 , zip4, zip5, zip6, zip7
73 , zipWith4, zipWith5, zipWith6, zipWith7
74 , unzip4, unzip5, unzip6, unzip7
76 , map -- :: ( a -> b ) -> [a] -> [b]
77 , (++) -- :: [a] -> [a] -> [a]
78 , concat -- :: [[a]] -> [a]
79 , filter -- :: (a -> Bool) -> [a] -> [a]
82 , tail -- :: [a] -> [a]
83 , init -- :: [a] -> [a]
84 , null -- :: [a] -> Bool
85 , length -- :: [a] -> Int
86 , (!!) -- :: [a] -> Int -> a
87 , foldl -- :: (a -> b -> a) -> a -> [b] -> a
88 , foldl1 -- :: (a -> a -> a) -> [a] -> a
89 , scanl -- :: (a -> b -> a) -> a -> [b] -> [a]
90 , scanl1 -- :: (a -> a -> a) -> [a] -> [a]
91 , foldr -- :: (a -> b -> b) -> b -> [a] -> b
92 , foldr1 -- :: (a -> a -> a) -> [a] -> a
93 , scanr -- :: (a -> b -> b) -> b -> [a] -> [b]
94 , scanr1 -- :: (a -> a -> a) -> [a] -> [a]
95 , iterate -- :: (a -> a) -> a -> [a]
96 , repeat -- :: a -> [a]
97 , replicate -- :: Int -> a -> [a]
98 , cycle -- :: [a] -> [a]
99 , take -- :: Int -> [a] -> [a]
100 , drop -- :: Int -> [a] -> [a]
101 , splitAt -- :: Int -> [a] -> ([a], [a])
102 , takeWhile -- :: (a -> Bool) -> [a] -> [a]
103 , dropWhile -- :: (a -> Bool) -> [a] -> [a]
104 , span -- :: (a -> Bool) -> [a] -> ([a], [a])
105 , break -- :: (a -> Bool) -> [a] -> ([a], [a])
107 , lines -- :: String -> [String]
108 , words -- :: String -> [String]
109 , unlines -- :: [String] -> String
110 , unwords -- :: [String] -> String
111 , reverse -- :: [a] -> [a]
112 , and -- :: [Bool] -> Bool
113 , or -- :: [Bool] -> Bool
114 , any -- :: (a -> Bool) -> [a] -> Bool
115 , all -- :: (a -> Bool) -> [a] -> Bool
116 , elem -- :: a -> [a] -> Bool
117 , notElem -- :: a -> [a] -> Bool
118 , lookup -- :: (Eq a) => a -> [(a,b)] -> Maybe b
119 , sum -- :: (Num a) => [a] -> a
120 , product -- :: (Num a) => [a] -> a
121 , maximum -- :: (Ord a) => [a] -> a
122 , minimum -- :: (Ord a) => [a] -> a
123 , concatMap -- :: (a -> [b]) -> [a] -> [b]
124 , zip -- :: [a] -> [b] -> [(a,b)]
126 , zipWith -- :: (a -> b -> c) -> [a] -> [b] -> [c]
128 , unzip -- :: [(a,b)] -> ([a],[b])
131 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
135 import Maybe ( listToMaybe )
138 import PrelShow ( lines, words, unlines, unwords )
139 import PrelBase ( Int(..), map, (++) )
140 import PrelGHC ( (+#) )
146 %*********************************************************
148 \subsection{List functions}
150 %*********************************************************
153 elemIndex :: Eq a => a -> [a] -> Maybe Int
154 elemIndex x = findIndex (x==)
156 elemIndices :: Eq a => a -> [a] -> [Int]
157 elemIndices x = findIndices (x==)
159 find :: (a -> Bool) -> [a] -> Maybe a
160 find p = listToMaybe . filter p
162 findIndex :: (a -> Bool) -> [a] -> Maybe Int
163 findIndex p = listToMaybe . findIndices p
165 findIndices :: (a -> Bool) -> [a] -> [Int]
167 #ifdef USE_REPORT_PRELUDE
168 findIndices p xs = [ i | (x,i) <- zip xs [0..], p x]
171 findIndices p xs = [ i | (x,i) <- zip xs [0..], p x]
173 -- Efficient definition
174 findIndices p ls = loop 0# ls
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 */
182 isPrefixOf :: (Eq a) => [a] -> [a] -> Bool
183 isPrefixOf [] _ = True
184 isPrefixOf _ [] = False
185 isPrefixOf (x:xs) (y:ys)= x == y && isPrefixOf xs ys
187 isSuffixOf :: (Eq a) => [a] -> [a] -> Bool
188 isSuffixOf x y = reverse x `isPrefixOf` reverse y
190 -- nub (meaning "essence") remove duplicate elements from its list argument.
191 nub :: (Eq a) => [a] -> [a]
192 #ifdef USE_REPORT_PRELUDE
196 nub l = nub' l [] -- '
200 | x `elem` ls = nub' xs ls -- '
201 | otherwise = x : nub' xs (x:ls) -- '
204 nubBy :: (a -> a -> Bool) -> [a] -> [a]
205 #ifdef USE_REPORT_PRELUDE
207 nubBy eq (x:xs) = x : nubBy eq (filter (\ y -> not (eq x y)) xs)
209 nubBy eq l = nubBy' l []
213 | elemBy eq x ls = nubBy' xs ls
214 | otherwise = x : nubBy' xs (x:ls)
217 elemBy :: (a -> a -> Bool) -> a -> [a] -> Bool
218 elemBy _ _ [] = False
219 elemBy eq x (y:ys) = x `eq` y || elemBy eq x ys
223 -- delete x removes the first occurrence of x from its list argument.
224 delete :: (Eq a) => a -> [a] -> [a]
225 delete = deleteBy (==)
227 deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a]
229 deleteBy eq x (y:ys) = if x `eq` y then ys else y : deleteBy eq x ys
231 -- list difference (non-associative). In the result of xs \\ ys,
232 -- the first occurrence of each element of ys in turn (if any)
233 -- has been removed from xs. Thus, (xs ++ ys) \\ xs == ys.
234 (\\) :: (Eq a) => [a] -> [a] -> [a]
235 (\\) = foldl (flip delete)
237 -- List union, remove the elements of first list from second.
238 union :: (Eq a) => [a] -> [a] -> [a]
241 unionBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
242 unionBy eq xs ys = xs ++ foldl (flip (deleteBy eq)) (nubBy eq ys) xs
244 intersect :: (Eq a) => [a] -> [a] -> [a]
245 intersect = intersectBy (==)
247 intersectBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
248 intersectBy eq xs ys = [x | x <- xs, any (eq x) ys]
250 -- intersperse sep inserts sep between the elements of its list argument.
251 -- e.g. intersperse ',' "abcde" == "a,b,c,d,e"
252 intersperse :: a -> [a] -> [a]
253 intersperse _ [] = []
254 intersperse _ [x] = [x]
255 intersperse sep (x:xs) = x : sep : intersperse sep xs
257 transpose :: [[a]] -> [[a]]
259 transpose ([] : xss) = transpose xss
260 transpose ((x:xs) : xss) = (x : [h | (h:t) <- xss]) : transpose (xs : [ t | (h:t) <- xss])
263 -- partition takes a predicate and a list and returns a pair of lists:
264 -- those elements of the argument list that do and do not satisfy the
265 -- predicate, respectively; i,e,,
266 -- partition p xs == (filter p xs, filter (not . p) xs).
267 partition :: (a -> Bool) -> [a] -> ([a],[a])
268 {-# INLINE partition #-}
269 partition p xs = foldr (select p) ([],[]) xs
271 select p x (ts,fs) | p x = (x:ts,fs)
272 | otherwise = (ts, x:fs)
275 @mapAccumL@ behaves like a combination
276 of @map@ and @foldl@;
277 it applies a function to each element of a list, passing an accumulating
278 parameter from left to right, and returning a final value of this
279 accumulator together with the new list.
283 mapAccumL :: (acc -> x -> (acc, y)) -- Function of elt of input list
284 -- and accumulator, returning new
285 -- accumulator and elt of result list
286 -> acc -- Initial accumulator
288 -> (acc, [y]) -- Final accumulator and result list
289 mapAccumL _ s [] = (s, [])
290 mapAccumL f s (x:xs) = (s'',y:ys)
291 where (s', y ) = f s x
292 (s'',ys) = mapAccumL f s' xs
295 @mapAccumR@ does the same, but working from right to left instead. Its type is
296 the same as @mapAccumL@, though.
299 mapAccumR :: (acc -> x -> (acc, y)) -- Function of elt of input list
300 -- and accumulator, returning new
301 -- accumulator and elt of result list
302 -> acc -- Initial accumulator
304 -> (acc, [y]) -- Final accumulator and result list
305 mapAccumR _ s [] = (s, [])
306 mapAccumR f s (x:xs) = (s'', y:ys)
307 where (s'',y ) = f s' x
308 (s', ys) = mapAccumR f s xs
312 insert :: Ord a => a -> [a] -> [a]
313 insert e ls = insertBy (compare) e ls
315 insertBy :: (a -> a -> Ordering) -> a -> [a] -> [a]
316 insertBy _ x [] = [x]
317 insertBy cmp x ys@(y:ys')
319 GT -> y : insertBy cmp x ys'
322 maximumBy :: (a -> a -> a) -> [a] -> a
323 maximumBy _ [] = error "List.maximumBy: empty list"
324 maximumBy max xs = foldl1 max xs
326 minimumBy :: (a -> a -> a) -> [a] -> a
327 minimumBy _ [] = error "List.minimumBy: empty list"
328 minimumBy min xs = foldl1 min xs
330 genericLength :: (Num i) => [b] -> i
332 genericLength (_:l) = 1 + genericLength l
334 genericTake :: (Integral i) => i -> [a] -> [a]
336 genericTake _ [] = []
337 genericTake n (x:xs) | n > 0 = x : genericTake (n-1) xs
338 genericTake _ _ = error "List.genericTake: negative argument"
340 genericDrop :: (Integral i) => i -> [a] -> [a]
341 genericDrop 0 xs = xs
342 genericDrop _ [] = []
343 genericDrop n (_:xs) | n > 0 = genericDrop (n-1) xs
344 genericDrop _ _ = error "List.genericDrop: negative argument"
346 genericSplitAt :: (Integral i) => i -> [b] -> ([b],[b])
347 genericSplitAt 0 xs = ([],xs)
348 genericSplitAt _ [] = ([],[])
349 genericSplitAt n (x:xs) | n > 0 = (x:xs',xs'') where
350 (xs',xs'') = genericSplitAt (n-1) xs
351 genericSplitAt _ _ = error "List.genericSplitAt: negative argument"
354 genericIndex :: (Integral a) => [b] -> a -> b
355 genericIndex (x:_) 0 = x
356 genericIndex (_:xs) n
357 | n > 0 = genericIndex xs (n-1)
358 | otherwise = error "List.genericIndex: negative argument."
359 genericIndex _ _ = error "List.genericIndex: index too large."
361 genericReplicate :: (Integral i) => i -> a -> [a]
362 genericReplicate n x = genericTake n (repeat x)
365 zip4 :: [a] -> [b] -> [c] -> [d] -> [(a,b,c,d)]
366 zip4 = zipWith4 (,,,)
368 zip5 :: [a] -> [b] -> [c] -> [d] -> [e] -> [(a,b,c,d,e)]
369 zip5 = zipWith5 (,,,,)
371 zip6 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] ->
373 zip6 = zipWith6 (,,,,,)
375 zip7 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] ->
376 [g] -> [(a,b,c,d,e,f,g)]
377 zip7 = zipWith7 (,,,,,,)
379 zipWith4 :: (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
380 zipWith4 z (a:as) (b:bs) (c:cs) (d:ds)
381 = z a b c d : zipWith4 z as bs cs ds
382 zipWith4 _ _ _ _ _ = []
384 zipWith5 :: (a->b->c->d->e->f) ->
385 [a]->[b]->[c]->[d]->[e]->[f]
386 zipWith5 z (a:as) (b:bs) (c:cs) (d:ds) (e:es)
387 = z a b c d e : zipWith5 z as bs cs ds es
388 zipWith5 _ _ _ _ _ _ = []
390 zipWith6 :: (a->b->c->d->e->f->g) ->
391 [a]->[b]->[c]->[d]->[e]->[f]->[g]
392 zipWith6 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs)
393 = z a b c d e f : zipWith6 z as bs cs ds es fs
394 zipWith6 _ _ _ _ _ _ _ = []
396 zipWith7 :: (a->b->c->d->e->f->g->h) ->
397 [a]->[b]->[c]->[d]->[e]->[f]->[g]->[h]
398 zipWith7 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs) (g:gs)
399 = z a b c d e f g : zipWith7 z as bs cs ds es fs gs
400 zipWith7 _ _ _ _ _ _ _ _ = []
402 unzip4 :: [(a,b,c,d)] -> ([a],[b],[c],[d])
403 unzip4 = foldr (\(a,b,c,d) ~(as,bs,cs,ds) ->
404 (a:as,b:bs,c:cs,d:ds))
407 unzip5 :: [(a,b,c,d,e)] -> ([a],[b],[c],[d],[e])
408 unzip5 = foldr (\(a,b,c,d,e) ~(as,bs,cs,ds,es) ->
409 (a:as,b:bs,c:cs,d:ds,e:es))
412 unzip6 :: [(a,b,c,d,e,f)] -> ([a],[b],[c],[d],[e],[f])
413 unzip6 = foldr (\(a,b,c,d,e,f) ~(as,bs,cs,ds,es,fs) ->
414 (a:as,b:bs,c:cs,d:ds,e:es,f:fs))
417 unzip7 :: [(a,b,c,d,e,f,g)] -> ([a],[b],[c],[d],[e],[f],[g])
418 unzip7 = foldr (\(a,b,c,d,e,f,g) ~(as,bs,cs,ds,es,fs,gs) ->
419 (a:as,b:bs,c:cs,d:ds,e:es,f:fs,g:gs))
420 ([],[],[],[],[],[],[])
424 deleteFirstsBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
425 deleteFirstsBy eq = foldl (flip (deleteBy eq))
428 -- group splits its list argument into a list of lists of equal, adjacent
430 -- group "Mississippi" == ["M","i","ss","i","ss","i","pp","i"]
431 group :: (Eq a) => [a] -> [[a]]
434 groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
436 groupBy eq (x:xs) = (x:ys) : groupBy eq zs
437 where (ys,zs) = span (eq x) xs
439 -- inits xs returns the list of initial segments of xs, shortest first.
440 -- e.g., inits "abc" == ["","a","ab","abc"]
441 inits :: [a] -> [[a]]
443 inits (x:xs) = [[]] ++ map (x:) (inits xs)
445 -- tails xs returns the list of all final segments of xs, longest first.
446 -- e.g., tails "abc" == ["abc", "bc", "c",""]
447 tails :: [a] -> [[a]]
449 tails xxs@(_:xs) = xxs : tails xs
453 %-----------------------------------------------------------------------------
454 Quick Sort algorithm taken from HBC's QSort library.
457 sort :: (Ord a) => [a] -> [a]
458 sortBy :: (a -> a -> Ordering) -> [a] -> [a]
460 #ifdef USE_REPORT_PRELUDE
461 sort = sortBy compare
462 sortBy cmp = foldr (insertBy cmp) []
465 sortBy cmp l = qsort cmp l []
466 sort l = qsort compare l []
468 -- rest is not exported:
470 -- qsort is stable and does not concatenate.
471 qsort :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
474 qsort cmp (x:xs) r = qpart cmp x xs [] [] r
476 -- qpart partitions and sorts the sublists
477 qpart :: (a -> a -> Ordering) -> a -> [a] -> [a] -> [a] -> [a] -> [a]
478 qpart cmp x [] rlt rge r =
479 -- rlt and rge are in reverse order and must be sorted with an
480 -- anti-stable sorting
481 rqsort cmp rlt (x:rqsort cmp rge r)
482 qpart cmp x (y:ys) rlt rge r =
484 GT -> qpart cmp x ys (y:rlt) rge r
485 _ -> qpart cmp x ys rlt (y:rge) r
487 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
488 rqsort :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
491 rqsort cmp (x:xs) r = rqpart cmp x xs [] [] r
493 rqpart :: (a -> a -> Ordering) -> a -> [a] -> [a] -> [a] -> [a] -> [a]
494 rqpart cmp x [] rle rgt r =
495 qsort cmp rle (x:qsort cmp rgt r)
496 rqpart cmp x (y:ys) rle rgt r =
498 GT -> rqpart cmp x ys rle (y:rgt) r
499 _ -> rqpart cmp x ys (y:rle) rgt r
501 #endif /* USE_REPORT_PRELUDE */
505 unfoldr f' (foldr f z xs) == (z,xs)
507 if the following holds:
509 f' (f x y) = Just (x,y)
514 unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
517 Just (a,new_b) -> a : unfoldr f new_b