2 % (c) The AQUA Project, Glasgow University, 1994-1999
5 \section[List]{Module @Lhar@}
12 , elemIndex -- :: (Eq a) => a -> [a] -> Maybe Int
13 , elemIndices -- :: (Eq a) => a -> [a] -> [Int]
15 , find -- :: (a -> Bool) -> [a] -> Maybe a
16 , findIndex -- :: (a -> Bool) -> [a] -> Maybe Int
17 , findIndices -- :: (a -> Bool) -> [a] -> [Int]
19 , nub -- :: (Eq a) => [a] -> [a]
20 , nubBy -- :: (a -> a -> Bool) -> [a] -> [a]
22 , delete -- :: (Eq a) => a -> [a] -> [a]
23 , deleteBy -- :: (a -> a -> Bool) -> a -> [a] -> [a]
24 , (\\) -- :: (Eq a) => [a] -> [a] -> [a]
25 , deleteFirstsBy -- :: (a -> a -> Bool) -> [a] -> [a] -> [a]
27 , union -- :: (Eq a) => [a] -> [a] -> [a]
28 , unionBy -- :: (a -> a -> Bool) -> [a] -> [a] -> [a]
30 , intersect -- :: (Eq a) => [a] -> [a] -> [a]
31 , intersectBy -- :: (a -> a -> Bool) -> [a] -> [a] -> [a]
33 , intersperse -- :: a -> [a] -> [a]
34 , transpose -- :: [[a]] -> [[a]]
35 , partition -- :: (a -> Bool) -> [a] -> ([a], [a])
37 , group -- :: Eq a => [a] -> [[a]]
38 , groupBy -- :: (a -> a -> Bool) -> [a] -> [[a]]
40 , inits -- :: [a] -> [[a]]
41 , tails -- :: [a] -> [[a]]
43 , isPrefixOf -- :: (Eq a) => [a] -> [a] -> Bool
44 , isSuffixOf -- :: (Eq a) => [a] -> [a] -> Bool
46 , mapAccumL -- :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c])
47 , mapAccumR -- :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c])
49 , sort -- :: (Ord a) => [a] -> [a]
50 , sortBy -- :: (a -> a -> Ordering) -> [a] -> [a]
52 , insert -- :: (Ord a) => a -> [a] -> [a]
53 , insertBy -- :: (a -> a -> Ordering) -> a -> [a] -> [a]
55 , maximumBy -- :: (a -> a -> Ordering) -> [a] -> a
56 , minimumBy -- :: (a -> a -> Ordering) -> [a] -> a
58 , genericLength -- :: (Integral a) => [b] -> a
59 , genericTake -- :: (Integral a) => a -> [b] -> [b]
60 , genericDrop -- :: (Integral a) => a -> [b] -> [b]
61 , genericSplitAt -- :: (Integral a) => a -> [b] -> ([b], [b])
62 , genericIndex -- :: (Integral a) => [b] -> a -> b
63 , genericReplicate -- :: (Integral a) => a -> b -> [b]
65 , unfoldr -- :: (a -> Maybe (b,a)) -> a -> (a,[b])
67 , zip4, zip5, zip6, zip7
68 , zipWith4, zipWith5, zipWith6, zipWith7
69 , unzip4, unzip5, unzip6, unzip7
71 , map -- :: ( a -> b ) -> [a] -> [b]
72 , (++) -- :: [a] -> [a] -> [a]
73 , concat -- :: [[a]] -> [a]
74 , filter -- :: (a -> Bool) -> [a] -> [a]
77 , tail -- :: [a] -> [a]
78 , init -- :: [a] -> [a]
79 , null -- :: [a] -> Bool
80 , length -- :: [a] -> Int
81 , (!!) -- :: [a] -> Int -> a
82 , foldl -- :: (a -> b -> a) -> a -> [b] -> a
83 , foldl1 -- :: (a -> a -> a) -> [a] -> a
84 , scanl -- :: (a -> b -> a) -> a -> [b] -> [a]
85 , scanl1 -- :: (a -> a -> a) -> [a] -> [a]
86 , foldr -- :: (a -> b -> b) -> b -> [a] -> b
87 , foldr1 -- :: (a -> a -> a) -> [a] -> a
88 , scanr -- :: (a -> b -> b) -> b -> [a] -> [b]
89 , scanr1 -- :: (a -> a -> a) -> [a] -> [a]
90 , iterate -- :: (a -> a) -> a -> [a]
91 , repeat -- :: a -> [a]
92 , replicate -- :: Int -> a -> [a]
93 , cycle -- :: [a] -> [a]
94 , take -- :: Int -> [a] -> [a]
95 , drop -- :: Int -> [a] -> [a]
96 , splitAt -- :: Int -> [a] -> ([a], [a])
97 , takeWhile -- :: (a -> Bool) -> [a] -> [a]
98 , dropWhile -- :: (a -> Bool) -> [a] -> [a]
99 , span -- :: (a -> Bool) -> [a] -> ([a], [a])
100 , break -- :: (a -> Bool) -> [a] -> ([a], [a])
102 , lines -- :: String -> [String]
103 , words -- :: String -> [String]
104 , unlines -- :: [String] -> String
105 , unwords -- :: [String] -> String
106 , reverse -- :: [a] -> [a]
107 , and -- :: [Bool] -> Bool
108 , or -- :: [Bool] -> Bool
109 , any -- :: (a -> Bool) -> [a] -> Bool
110 , all -- :: (a -> Bool) -> [a] -> Bool
111 , elem -- :: a -> [a] -> Bool
112 , notElem -- :: a -> [a] -> Bool
113 , lookup -- :: (Eq a) => a -> [(a,b)] -> Maybe b
114 , sum -- :: (Num a) => [a] -> a
115 , product -- :: (Num a) => [a] -> a
116 , maximum -- :: (Ord a) => [a] -> a
117 , minimum -- :: (Ord a) => [a] -> a
118 , concatMap -- :: (a -> [b]) -> [a] -> [b]
119 , zip -- :: [a] -> [b] -> [(a,b)]
121 , zipWith -- :: (a -> b -> c) -> [a] -> [b] -> [c]
123 , unzip -- :: [(a,b)] -> ([a],[b])
126 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
130 import PrelShow ( lines, words, unlines, unwords )
131 import Maybe ( listToMaybe )
132 import PrelBase ( Int(..), map, (++) )
133 import PrelGHC ( (+#) )
138 %*********************************************************
140 \subsection{List functions}
142 %*********************************************************
145 elemIndex :: Eq a => a -> [a] -> Maybe Int
146 elemIndex x = findIndex (x==)
148 elemIndices :: Eq a => a -> [a] -> [Int]
149 elemIndices x = findIndices (x==)
151 find :: (a -> Bool) -> [a] -> Maybe a
152 find p = listToMaybe . filter p
154 findIndex :: (a -> Bool) -> [a] -> Maybe Int
155 findIndex p = listToMaybe . findIndices p
157 findIndices :: (a -> Bool) -> [a] -> [Int]
159 #ifdef USE_REPORT_PRELUDE
160 findIndices p xs = [ i | (x,i) <- zip xs [0..], p x]
162 -- Efficient definition
163 findIndices p ls = loop 0# ls
166 loop n (x:xs) | p x = I# n : loop (n +# 1#) xs
167 | otherwise = loop (n +# 1#) xs
170 isPrefixOf :: (Eq a) => [a] -> [a] -> Bool
171 isPrefixOf [] _ = True
172 isPrefixOf _ [] = False
173 isPrefixOf (x:xs) (y:ys)= x == y && isPrefixOf xs ys
175 isSuffixOf :: (Eq a) => [a] -> [a] -> Bool
176 isSuffixOf x y = reverse x `isPrefixOf` reverse y
178 -- nub (meaning "essence") remove duplicate elements from its list argument.
179 nub :: (Eq a) => [a] -> [a]
180 #ifdef USE_REPORT_PRELUDE
188 | x `elem` ls = nub' xs ls
189 | otherwise = x : nub' xs (x:ls)
192 nubBy :: (a -> a -> Bool) -> [a] -> [a]
193 #ifdef USE_REPORT_PRELUDE
195 nubBy eq (x:xs) = x : nubBy eq (filter (\ y -> not (eq x y)) xs)
197 nubBy eq l = nubBy' l []
201 | elemBy eq x ls = nubBy' xs ls
202 | otherwise = x : nubBy' xs (x:ls)
205 elemBy :: (a -> a -> Bool) -> a -> [a] -> Bool
206 elemBy _ _ [] = False
207 elemBy eq x (y:ys) = x `eq` y || elemBy eq x ys
211 -- delete x removes the first occurrence of x from its list argument.
212 delete :: (Eq a) => a -> [a] -> [a]
213 delete = deleteBy (==)
215 deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a]
217 deleteBy eq x (y:ys) = if x `eq` y then ys else y : deleteBy eq x ys
219 -- list difference (non-associative). In the result of xs \\ ys,
220 -- the first occurrence of each element of ys in turn (if any)
221 -- has been removed from xs. Thus, (xs ++ ys) \\ xs == ys.
222 (\\) :: (Eq a) => [a] -> [a] -> [a]
223 (\\) = foldl (flip delete)
225 -- List union, remove the elements of first list from second.
226 union :: (Eq a) => [a] -> [a] -> [a]
229 unionBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
230 unionBy eq xs ys = xs ++ foldl (flip (deleteBy eq)) (nubBy eq ys) xs
232 intersect :: (Eq a) => [a] -> [a] -> [a]
233 intersect = intersectBy (==)
235 intersectBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
236 intersectBy eq xs ys = [x | x <- xs, any (eq x) ys]
238 -- intersperse sep inserts sep between the elements of its list argument.
239 -- e.g. intersperse ',' "abcde" == "a,b,c,d,e"
240 intersperse :: a -> [a] -> [a]
241 intersperse _ [] = []
242 intersperse _ [x] = [x]
243 intersperse sep (x:xs) = x : sep : intersperse sep xs
245 transpose :: [[a]] -> [[a]]
247 transpose ([] : xss) = transpose xss
248 transpose ((x:xs) : xss) = (x : [h | (h:t) <- xss]) : transpose (xs : [ t | (h:t) <- xss])
251 -- partition takes a predicate and a list and returns a pair of lists:
252 -- those elements of the argument list that do and do not satisfy the
253 -- predicate, respectively; i,e,,
254 -- partition p xs == (filter p xs, filter (not . p) xs).
255 partition :: (a -> Bool) -> [a] -> ([a],[a])
256 {-# INLINE partition #-}
257 partition p xs = foldr (select p) ([],[]) xs
259 select p x (ts,fs) | p x = (x:ts,fs)
260 | otherwise = (ts, x:fs)
263 @mapAccumL@ behaves like a combination
264 of @map@ and @foldl@;
265 it applies a function to each element of a list, passing an accumulating
266 parameter from left to right, and returning a final value of this
267 accumulator together with the new list.
271 mapAccumL :: (acc -> x -> (acc, y)) -- Function of elt of input list
272 -- and accumulator, returning new
273 -- accumulator and elt of result list
274 -> acc -- Initial accumulator
276 -> (acc, [y]) -- Final accumulator and result list
277 mapAccumL _ s [] = (s, [])
278 mapAccumL f s (x:xs) = (s'',y:ys)
279 where (s', y ) = f s x
280 (s'',ys) = mapAccumL f s' xs
283 @mapAccumR@ does the same, but working from right to left instead. Its type is
284 the same as @mapAccumL@, though.
287 mapAccumR :: (acc -> x -> (acc, y)) -- Function of elt of input list
288 -- and accumulator, returning new
289 -- accumulator and elt of result list
290 -> acc -- Initial accumulator
292 -> (acc, [y]) -- Final accumulator and result list
293 mapAccumR _ s [] = (s, [])
294 mapAccumR f s (x:xs) = (s'', y:ys)
295 where (s'',y ) = f s' x
296 (s', ys) = mapAccumR f s xs
300 insert :: Ord a => a -> [a] -> [a]
301 insert e ls = insertBy (compare) e ls
303 insertBy :: (a -> a -> Ordering) -> a -> [a] -> [a]
304 insertBy _ x [] = [x]
305 insertBy cmp x ys@(y:ys')
307 GT -> y : insertBy cmp x ys'
310 maximumBy :: (a -> a -> a) -> [a] -> a
311 maximumBy _ [] = error "List.maximumBy: empty list"
312 maximumBy max xs = foldl1 max xs
314 minimumBy :: (a -> a -> a) -> [a] -> a
315 minimumBy _ [] = error "List.minimumBy: empty list"
316 minimumBy min xs = foldl1 min xs
318 genericLength :: (Num i) => [b] -> i
320 genericLength (_:l) = 1 + genericLength l
322 genericTake :: (Integral i) => i -> [a] -> [a]
324 genericTake _ [] = []
325 genericTake n (x:xs) | n > 0 = x : genericTake (n-1) xs
326 genericTake _ _ = error "List.genericTake: negative argument"
328 genericDrop :: (Integral i) => i -> [a] -> [a]
329 genericDrop 0 xs = xs
330 genericDrop _ [] = []
331 genericDrop n (_:xs) | n > 0 = genericDrop (n-1) xs
332 genericDrop _ _ = error "List.genericDrop: negative argument"
334 genericSplitAt :: (Integral i) => i -> [b] -> ([b],[b])
335 genericSplitAt 0 xs = ([],xs)
336 genericSplitAt _ [] = ([],[])
337 genericSplitAt n (x:xs) | n > 0 = (x:xs',xs'') where
338 (xs',xs'') = genericSplitAt (n-1) xs
339 genericSplitAt _ _ = error "List.genericSplitAt: negative argument"
342 genericIndex :: (Integral a) => [b] -> a -> b
343 genericIndex (x:_) 0 = x
344 genericIndex (_:xs) n
345 | n > 0 = genericIndex xs (n-1)
346 | otherwise = error "List.genericIndex: negative argument."
347 genericIndex _ _ = error "List.genericIndex: index too large."
349 genericReplicate :: (Integral i) => i -> a -> [a]
350 genericReplicate n x = genericTake n (repeat x)
353 zip4 :: [a] -> [b] -> [c] -> [d] -> [(a,b,c,d)]
354 zip4 = zipWith4 (,,,)
356 zip5 :: [a] -> [b] -> [c] -> [d] -> [e] -> [(a,b,c,d,e)]
357 zip5 = zipWith5 (,,,,)
359 zip6 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] ->
361 zip6 = zipWith6 (,,,,,)
363 zip7 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] ->
364 [g] -> [(a,b,c,d,e,f,g)]
365 zip7 = zipWith7 (,,,,,,)
367 zipWith4 :: (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
368 zipWith4 z (a:as) (b:bs) (c:cs) (d:ds)
369 = z a b c d : zipWith4 z as bs cs ds
370 zipWith4 _ _ _ _ _ = []
372 zipWith5 :: (a->b->c->d->e->f) ->
373 [a]->[b]->[c]->[d]->[e]->[f]
374 zipWith5 z (a:as) (b:bs) (c:cs) (d:ds) (e:es)
375 = z a b c d e : zipWith5 z as bs cs ds es
376 zipWith5 _ _ _ _ _ _ = []
378 zipWith6 :: (a->b->c->d->e->f->g) ->
379 [a]->[b]->[c]->[d]->[e]->[f]->[g]
380 zipWith6 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs)
381 = z a b c d e f : zipWith6 z as bs cs ds es fs
382 zipWith6 _ _ _ _ _ _ _ = []
384 zipWith7 :: (a->b->c->d->e->f->g->h) ->
385 [a]->[b]->[c]->[d]->[e]->[f]->[g]->[h]
386 zipWith7 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs) (g:gs)
387 = z a b c d e f g : zipWith7 z as bs cs ds es fs gs
388 zipWith7 _ _ _ _ _ _ _ _ = []
390 unzip4 :: [(a,b,c,d)] -> ([a],[b],[c],[d])
391 unzip4 = foldr (\(a,b,c,d) ~(as,bs,cs,ds) ->
392 (a:as,b:bs,c:cs,d:ds))
395 unzip5 :: [(a,b,c,d,e)] -> ([a],[b],[c],[d],[e])
396 unzip5 = foldr (\(a,b,c,d,e) ~(as,bs,cs,ds,es) ->
397 (a:as,b:bs,c:cs,d:ds,e:es))
400 unzip6 :: [(a,b,c,d,e,f)] -> ([a],[b],[c],[d],[e],[f])
401 unzip6 = foldr (\(a,b,c,d,e,f) ~(as,bs,cs,ds,es,fs) ->
402 (a:as,b:bs,c:cs,d:ds,e:es,f:fs))
405 unzip7 :: [(a,b,c,d,e,f,g)] -> ([a],[b],[c],[d],[e],[f],[g])
406 unzip7 = foldr (\(a,b,c,d,e,f,g) ~(as,bs,cs,ds,es,fs,gs) ->
407 (a:as,b:bs,c:cs,d:ds,e:es,f:fs,g:gs))
408 ([],[],[],[],[],[],[])
412 deleteFirstsBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
413 deleteFirstsBy eq = foldl (flip (deleteBy eq))
416 -- group splits its list argument into a list of lists of equal, adjacent
418 -- group "Mississippi" == ["M","i","ss","i","ss","i","pp","i"]
419 group :: (Eq a) => [a] -> [[a]]
422 groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
424 groupBy eq (x:xs) = (x:ys) : groupBy eq zs
425 where (ys,zs) = span (eq x) xs
427 -- inits xs returns the list of initial segments of xs, shortest first.
428 -- e.g., inits "abc" == ["","a","ab","abc"]
429 inits :: [a] -> [[a]]
431 inits (x:xs) = [[]] ++ map (x:) (inits xs)
433 -- tails xs returns the list of all final segments of xs, longest first.
434 -- e.g., tails "abc" == ["abc", "bc", "c",""]
435 tails :: [a] -> [[a]]
437 tails xxs@(_:xs) = xxs : tails xs
441 %-----------------------------------------------------------------------------
442 Quick Sort algorithm taken from HBC's QSort library.
445 sort :: (Ord a) => [a] -> [a]
446 sortBy :: (a -> a -> Ordering) -> [a] -> [a]
448 #ifdef USE_REPORT_PRELUDE
449 sort = sortBy compare
450 sortBy cmp = foldr (insertBy cmp) []
453 sortBy cmp l = qsort cmp l []
454 sort l = qsort compare l []
456 -- rest is not exported:
458 -- qsort is stable and does not concatenate.
459 qsort :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
462 qsort cmp (x:xs) r = qpart cmp x xs [] [] r
464 -- qpart partitions and sorts the sublists
465 qpart :: (a -> a -> Ordering) -> a -> [a] -> [a] -> [a] -> [a] -> [a]
466 qpart cmp x [] rlt rge r =
467 -- rlt and rge are in reverse order and must be sorted with an
468 -- anti-stable sorting
469 rqsort cmp rlt (x:rqsort cmp rge r)
470 qpart cmp x (y:ys) rlt rge r =
472 GT -> qpart cmp x ys (y:rlt) rge r
473 _ -> qpart cmp x ys rlt (y:rge) r
475 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
476 rqsort :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
479 rqsort cmp (x:xs) r = rqpart cmp x xs [] [] r
481 rqpart :: (a -> a -> Ordering) -> a -> [a] -> [a] -> [a] -> [a] -> [a]
482 rqpart cmp x [] rle rgt r =
483 qsort cmp rle (x:qsort cmp rgt r)
484 rqpart cmp x (y:ys) rle rgt r =
486 GT -> rqpart cmp x ys rle (y:rgt) r
487 _ -> rqpart cmp x ys (y:rle) rgt r
489 #endif /* USE_REPORT_PRELUDE */
493 unfoldr f' (foldr f z xs) == (z,xs)
495 if the following holds:
497 f' (f x y) = Just (x,y)
502 unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
505 Just (a,new_b) -> a : unfoldr f new_b