2 % (c) The AQUA Project, Glasgow University, 1994-1999
5 \section[List]{Module @Lhar@}
15 elemIndex -- :: (Eq a) => a -> [a] -> Maybe Int
16 , elemIndices -- :: (Eq a) => a -> [a] -> [Int]
18 , find -- :: (a -> Bool) -> [a] -> Maybe a
19 , findIndex -- :: (a -> Bool) -> [a] -> Maybe Int
20 , findIndices -- :: (a -> Bool) -> [a] -> [Int]
22 , nub -- :: (Eq a) => [a] -> [a]
23 , nubBy -- :: (a -> a -> Bool) -> [a] -> [a]
25 , delete -- :: (Eq a) => a -> [a] -> [a]
26 , deleteBy -- :: (a -> a -> Bool) -> a -> [a] -> [a]
27 , (\\) -- :: (Eq a) => [a] -> [a] -> [a]
28 , deleteFirstsBy -- :: (a -> a -> Bool) -> [a] -> [a] -> [a]
30 , union -- :: (Eq a) => [a] -> [a] -> [a]
31 , unionBy -- :: (a -> a -> Bool) -> [a] -> [a] -> [a]
33 , intersect -- :: (Eq a) => [a] -> [a] -> [a]
34 , intersectBy -- :: (a -> a -> Bool) -> [a] -> [a] -> [a]
36 , intersperse -- :: a -> [a] -> [a]
37 , transpose -- :: [[a]] -> [[a]]
38 , partition -- :: (a -> Bool) -> [a] -> ([a], [a])
40 , group -- :: Eq a => [a] -> [[a]]
41 , groupBy -- :: (a -> a -> Bool) -> [a] -> [[a]]
43 , inits -- :: [a] -> [[a]]
44 , tails -- :: [a] -> [[a]]
46 , isPrefixOf -- :: (Eq a) => [a] -> [a] -> Bool
47 , isSuffixOf -- :: (Eq a) => [a] -> [a] -> Bool
49 , mapAccumL -- :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c])
50 , mapAccumR -- :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c])
52 , sort -- :: (Ord a) => [a] -> [a]
53 , sortBy -- :: (a -> a -> Ordering) -> [a] -> [a]
55 , insert -- :: (Ord a) => a -> [a] -> [a]
56 , insertBy -- :: (a -> a -> Ordering) -> a -> [a] -> [a]
58 , maximumBy -- :: (a -> a -> Ordering) -> [a] -> a
59 , minimumBy -- :: (a -> a -> Ordering) -> [a] -> a
61 , genericLength -- :: (Integral a) => [b] -> a
62 , genericTake -- :: (Integral a) => a -> [b] -> [b]
63 , genericDrop -- :: (Integral a) => a -> [b] -> [b]
64 , genericSplitAt -- :: (Integral a) => a -> [b] -> ([b], [b])
65 , genericIndex -- :: (Integral a) => [b] -> a -> b
66 , genericReplicate -- :: (Integral a) => a -> b -> [b]
68 , unfoldr -- :: (a -> Maybe (b,a)) -> a -> (a,[b])
70 , zip4, zip5, zip6, zip7
71 , zipWith4, zipWith5, zipWith6, zipWith7
72 , unzip4, unzip5, unzip6, unzip7
74 , map -- :: ( a -> b ) -> [a] -> [b]
75 , (++) -- :: [a] -> [a] -> [a]
76 , concat -- :: [[a]] -> [a]
77 , filter -- :: (a -> Bool) -> [a] -> [a]
80 , tail -- :: [a] -> [a]
81 , init -- :: [a] -> [a]
82 , null -- :: [a] -> Bool
83 , length -- :: [a] -> Int
84 , (!!) -- :: [a] -> Int -> a
85 , foldl -- :: (a -> b -> a) -> a -> [b] -> a
86 , foldl1 -- :: (a -> a -> a) -> [a] -> a
87 , scanl -- :: (a -> b -> a) -> a -> [b] -> [a]
88 , scanl1 -- :: (a -> a -> a) -> [a] -> [a]
89 , foldr -- :: (a -> b -> b) -> b -> [a] -> b
90 , foldr1 -- :: (a -> a -> a) -> [a] -> a
91 , scanr -- :: (a -> b -> b) -> b -> [a] -> [b]
92 , scanr1 -- :: (a -> a -> a) -> [a] -> [a]
93 , iterate -- :: (a -> a) -> a -> [a]
94 , repeat -- :: a -> [a]
95 , replicate -- :: Int -> a -> [a]
96 , cycle -- :: [a] -> [a]
97 , take -- :: Int -> [a] -> [a]
98 , drop -- :: Int -> [a] -> [a]
99 , splitAt -- :: Int -> [a] -> ([a], [a])
100 , takeWhile -- :: (a -> Bool) -> [a] -> [a]
101 , dropWhile -- :: (a -> Bool) -> [a] -> [a]
102 , span -- :: (a -> Bool) -> [a] -> ([a], [a])
103 , break -- :: (a -> Bool) -> [a] -> ([a], [a])
105 , lines -- :: String -> [String]
106 , words -- :: String -> [String]
107 , unlines -- :: [String] -> String
108 , unwords -- :: [String] -> String
109 , reverse -- :: [a] -> [a]
110 , and -- :: [Bool] -> Bool
111 , or -- :: [Bool] -> Bool
112 , any -- :: (a -> Bool) -> [a] -> Bool
113 , all -- :: (a -> Bool) -> [a] -> Bool
114 , elem -- :: a -> [a] -> Bool
115 , notElem -- :: a -> [a] -> Bool
116 , lookup -- :: (Eq a) => a -> [(a,b)] -> Maybe b
117 , sum -- :: (Num a) => [a] -> a
118 , product -- :: (Num a) => [a] -> a
119 , maximum -- :: (Ord a) => [a] -> a
120 , minimum -- :: (Ord a) => [a] -> a
121 , concatMap -- :: (a -> [b]) -> [a] -> [b]
122 , zip -- :: [a] -> [b] -> [(a,b)]
124 , zipWith -- :: (a -> b -> c) -> [a] -> [b] -> [c]
126 , unzip -- :: [(a,b)] -> ([a],[b])
129 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
133 import Maybe ( listToMaybe )
136 import PrelShow ( lines, words, unlines, unwords )
137 import PrelBase ( Int(..), map, (++) )
138 import PrelGHC ( (+#) )
144 %*********************************************************
146 \subsection{List functions}
148 %*********************************************************
151 elemIndex :: Eq a => a -> [a] -> Maybe Int
152 elemIndex x = findIndex (x==)
154 elemIndices :: Eq a => a -> [a] -> [Int]
155 elemIndices x = findIndices (x==)
157 find :: (a -> Bool) -> [a] -> Maybe a
158 find p = listToMaybe . filter p
160 findIndex :: (a -> Bool) -> [a] -> Maybe Int
161 findIndex p = listToMaybe . findIndices p
163 findIndices :: (a -> Bool) -> [a] -> [Int]
165 #ifdef USE_REPORT_PRELUDE
166 findIndices p xs = [ i | (x,i) <- zip xs [0..], p x]
169 findIndices p xs = [ i | (x,i) <- zip xs [0..], p x]
171 -- Efficient definition
172 findIndices p ls = loop 0# ls
175 loop n (x:xs) | p x = I# n : loop (n +# 1#) xs
176 | otherwise = loop (n +# 1#) xs
177 #endif /* __HUGS__ */
178 #endif /* USE_REPORT_PRELUDE */
180 isPrefixOf :: (Eq a) => [a] -> [a] -> Bool
181 isPrefixOf [] _ = True
182 isPrefixOf _ [] = False
183 isPrefixOf (x:xs) (y:ys)= x == y && isPrefixOf xs ys
185 isSuffixOf :: (Eq a) => [a] -> [a] -> Bool
186 isSuffixOf x y = reverse x `isPrefixOf` reverse y
188 -- nub (meaning "essence") remove duplicate elements from its list argument.
189 nub :: (Eq a) => [a] -> [a]
190 #ifdef USE_REPORT_PRELUDE
194 nub l = nub' l [] -- '
198 | x `elem` ls = nub' xs ls -- '
199 | otherwise = x : nub' xs (x:ls) -- '
202 nubBy :: (a -> a -> Bool) -> [a] -> [a]
203 #ifdef USE_REPORT_PRELUDE
205 nubBy eq (x:xs) = x : nubBy eq (filter (\ y -> not (eq x y)) xs)
207 nubBy eq l = nubBy' l []
211 | elemBy eq x ls = nubBy' xs ls
212 | otherwise = x : nubBy' xs (x:ls)
215 elemBy :: (a -> a -> Bool) -> a -> [a] -> Bool
216 elemBy _ _ [] = False
217 elemBy eq x (y:ys) = x `eq` y || elemBy eq x ys
221 -- delete x removes the first occurrence of x from its list argument.
222 delete :: (Eq a) => a -> [a] -> [a]
223 delete = deleteBy (==)
225 deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a]
227 deleteBy eq x (y:ys) = if x `eq` y then ys else y : deleteBy eq x ys
229 -- list difference (non-associative). In the result of xs \\ ys,
230 -- the first occurrence of each element of ys in turn (if any)
231 -- has been removed from xs. Thus, (xs ++ ys) \\ xs == ys.
232 (\\) :: (Eq a) => [a] -> [a] -> [a]
233 (\\) = foldl (flip delete)
235 -- List union, remove the elements of first list from second.
236 union :: (Eq a) => [a] -> [a] -> [a]
239 unionBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
240 unionBy eq xs ys = xs ++ foldl (flip (deleteBy eq)) (nubBy eq ys) xs
242 intersect :: (Eq a) => [a] -> [a] -> [a]
243 intersect = intersectBy (==)
245 intersectBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
246 intersectBy eq xs ys = [x | x <- xs, any (eq x) ys]
248 -- intersperse sep inserts sep between the elements of its list argument.
249 -- e.g. intersperse ',' "abcde" == "a,b,c,d,e"
250 intersperse :: a -> [a] -> [a]
251 intersperse _ [] = []
252 intersperse _ [x] = [x]
253 intersperse sep (x:xs) = x : sep : intersperse sep xs
255 transpose :: [[a]] -> [[a]]
257 transpose ([] : xss) = transpose xss
258 transpose ((x:xs) : xss) = (x : [h | (h:t) <- xss]) : transpose (xs : [ t | (h:t) <- xss])
261 -- partition takes a predicate and a list and returns a pair of lists:
262 -- those elements of the argument list that do and do not satisfy the
263 -- predicate, respectively; i,e,,
264 -- partition p xs == (filter p xs, filter (not . p) xs).
265 partition :: (a -> Bool) -> [a] -> ([a],[a])
266 {-# INLINE partition #-}
267 partition p xs = foldr (select p) ([],[]) xs
269 select p x (ts,fs) | p x = (x:ts,fs)
270 | otherwise = (ts, x:fs)
273 @mapAccumL@ behaves like a combination
274 of @map@ and @foldl@;
275 it applies a function to each element of a list, passing an accumulating
276 parameter from left to right, and returning a final value of this
277 accumulator together with the new list.
281 mapAccumL :: (acc -> x -> (acc, y)) -- Function of elt of input list
282 -- and accumulator, returning new
283 -- accumulator and elt of result list
284 -> acc -- Initial accumulator
286 -> (acc, [y]) -- Final accumulator and result list
287 mapAccumL _ s [] = (s, [])
288 mapAccumL f s (x:xs) = (s'',y:ys)
289 where (s', y ) = f s x
290 (s'',ys) = mapAccumL f s' xs
293 @mapAccumR@ does the same, but working from right to left instead. Its type is
294 the same as @mapAccumL@, though.
297 mapAccumR :: (acc -> x -> (acc, y)) -- Function of elt of input list
298 -- and accumulator, returning new
299 -- accumulator and elt of result list
300 -> acc -- Initial accumulator
302 -> (acc, [y]) -- Final accumulator and result list
303 mapAccumR _ s [] = (s, [])
304 mapAccumR f s (x:xs) = (s'', y:ys)
305 where (s'',y ) = f s' x
306 (s', ys) = mapAccumR f s xs
310 insert :: Ord a => a -> [a] -> [a]
311 insert e ls = insertBy (compare) e ls
313 insertBy :: (a -> a -> Ordering) -> a -> [a] -> [a]
314 insertBy _ x [] = [x]
315 insertBy cmp x ys@(y:ys')
317 GT -> y : insertBy cmp x ys'
320 maximumBy :: (a -> a -> a) -> [a] -> a
321 maximumBy _ [] = error "List.maximumBy: empty list"
322 maximumBy max xs = foldl1 max xs
324 minimumBy :: (a -> a -> a) -> [a] -> a
325 minimumBy _ [] = error "List.minimumBy: empty list"
326 minimumBy min xs = foldl1 min xs
328 genericLength :: (Num i) => [b] -> i
330 genericLength (_:l) = 1 + genericLength l
332 genericTake :: (Integral i) => i -> [a] -> [a]
334 genericTake _ [] = []
335 genericTake n (x:xs) | n > 0 = x : genericTake (n-1) xs
336 genericTake _ _ = error "List.genericTake: negative argument"
338 genericDrop :: (Integral i) => i -> [a] -> [a]
339 genericDrop 0 xs = xs
340 genericDrop _ [] = []
341 genericDrop n (_:xs) | n > 0 = genericDrop (n-1) xs
342 genericDrop _ _ = error "List.genericDrop: negative argument"
344 genericSplitAt :: (Integral i) => i -> [b] -> ([b],[b])
345 genericSplitAt 0 xs = ([],xs)
346 genericSplitAt _ [] = ([],[])
347 genericSplitAt n (x:xs) | n > 0 = (x:xs',xs'') where
348 (xs',xs'') = genericSplitAt (n-1) xs
349 genericSplitAt _ _ = error "List.genericSplitAt: negative argument"
352 genericIndex :: (Integral a) => [b] -> a -> b
353 genericIndex (x:_) 0 = x
354 genericIndex (_:xs) n
355 | n > 0 = genericIndex xs (n-1)
356 | otherwise = error "List.genericIndex: negative argument."
357 genericIndex _ _ = error "List.genericIndex: index too large."
359 genericReplicate :: (Integral i) => i -> a -> [a]
360 genericReplicate n x = genericTake n (repeat x)
363 zip4 :: [a] -> [b] -> [c] -> [d] -> [(a,b,c,d)]
364 zip4 = zipWith4 (,,,)
366 zip5 :: [a] -> [b] -> [c] -> [d] -> [e] -> [(a,b,c,d,e)]
367 zip5 = zipWith5 (,,,,)
369 zip6 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] ->
371 zip6 = zipWith6 (,,,,,)
373 zip7 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] ->
374 [g] -> [(a,b,c,d,e,f,g)]
375 zip7 = zipWith7 (,,,,,,)
377 zipWith4 :: (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
378 zipWith4 z (a:as) (b:bs) (c:cs) (d:ds)
379 = z a b c d : zipWith4 z as bs cs ds
380 zipWith4 _ _ _ _ _ = []
382 zipWith5 :: (a->b->c->d->e->f) ->
383 [a]->[b]->[c]->[d]->[e]->[f]
384 zipWith5 z (a:as) (b:bs) (c:cs) (d:ds) (e:es)
385 = z a b c d e : zipWith5 z as bs cs ds es
386 zipWith5 _ _ _ _ _ _ = []
388 zipWith6 :: (a->b->c->d->e->f->g) ->
389 [a]->[b]->[c]->[d]->[e]->[f]->[g]
390 zipWith6 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs)
391 = z a b c d e f : zipWith6 z as bs cs ds es fs
392 zipWith6 _ _ _ _ _ _ _ = []
394 zipWith7 :: (a->b->c->d->e->f->g->h) ->
395 [a]->[b]->[c]->[d]->[e]->[f]->[g]->[h]
396 zipWith7 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs) (g:gs)
397 = z a b c d e f g : zipWith7 z as bs cs ds es fs gs
398 zipWith7 _ _ _ _ _ _ _ _ = []
400 unzip4 :: [(a,b,c,d)] -> ([a],[b],[c],[d])
401 unzip4 = foldr (\(a,b,c,d) ~(as,bs,cs,ds) ->
402 (a:as,b:bs,c:cs,d:ds))
405 unzip5 :: [(a,b,c,d,e)] -> ([a],[b],[c],[d],[e])
406 unzip5 = foldr (\(a,b,c,d,e) ~(as,bs,cs,ds,es) ->
407 (a:as,b:bs,c:cs,d:ds,e:es))
410 unzip6 :: [(a,b,c,d,e,f)] -> ([a],[b],[c],[d],[e],[f])
411 unzip6 = foldr (\(a,b,c,d,e,f) ~(as,bs,cs,ds,es,fs) ->
412 (a:as,b:bs,c:cs,d:ds,e:es,f:fs))
415 unzip7 :: [(a,b,c,d,e,f,g)] -> ([a],[b],[c],[d],[e],[f],[g])
416 unzip7 = foldr (\(a,b,c,d,e,f,g) ~(as,bs,cs,ds,es,fs,gs) ->
417 (a:as,b:bs,c:cs,d:ds,e:es,f:fs,g:gs))
418 ([],[],[],[],[],[],[])
422 deleteFirstsBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
423 deleteFirstsBy eq = foldl (flip (deleteBy eq))
426 -- group splits its list argument into a list of lists of equal, adjacent
428 -- group "Mississippi" == ["M","i","ss","i","ss","i","pp","i"]
429 group :: (Eq a) => [a] -> [[a]]
432 groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
434 groupBy eq (x:xs) = (x:ys) : groupBy eq zs
435 where (ys,zs) = span (eq x) xs
437 -- inits xs returns the list of initial segments of xs, shortest first.
438 -- e.g., inits "abc" == ["","a","ab","abc"]
439 inits :: [a] -> [[a]]
441 inits (x:xs) = [[]] ++ map (x:) (inits xs)
443 -- tails xs returns the list of all final segments of xs, longest first.
444 -- e.g., tails "abc" == ["abc", "bc", "c",""]
445 tails :: [a] -> [[a]]
447 tails xxs@(_:xs) = xxs : tails xs
451 %-----------------------------------------------------------------------------
452 Quick Sort algorithm taken from HBC's QSort library.
455 sort :: (Ord a) => [a] -> [a]
456 sortBy :: (a -> a -> Ordering) -> [a] -> [a]
458 #ifdef USE_REPORT_PRELUDE
459 sort = sortBy compare
460 sortBy cmp = foldr (insertBy cmp) []
463 sortBy cmp l = qsort cmp l []
464 sort l = qsort compare l []
466 -- rest is not exported:
468 -- qsort is stable and does not concatenate.
469 qsort :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
472 qsort cmp (x:xs) r = qpart cmp x xs [] [] r
474 -- qpart partitions and sorts the sublists
475 qpart :: (a -> a -> Ordering) -> a -> [a] -> [a] -> [a] -> [a] -> [a]
476 qpart cmp x [] rlt rge r =
477 -- rlt and rge are in reverse order and must be sorted with an
478 -- anti-stable sorting
479 rqsort cmp rlt (x:rqsort cmp rge r)
480 qpart cmp x (y:ys) rlt rge r =
482 GT -> qpart cmp x ys (y:rlt) rge r
483 _ -> qpart cmp x ys rlt (y:rge) r
485 -- rqsort is as qsort but anti-stable, i.e. reverses equal elements
486 rqsort :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
489 rqsort cmp (x:xs) r = rqpart cmp x xs [] [] r
491 rqpart :: (a -> a -> Ordering) -> a -> [a] -> [a] -> [a] -> [a] -> [a]
492 rqpart cmp x [] rle rgt r =
493 qsort cmp rle (x:qsort cmp rgt r)
494 rqpart cmp x (y:ys) rle rgt r =
496 GT -> rqpart cmp x ys rle (y:rgt) r
497 _ -> rqpart cmp x ys (y:rle) rgt r
499 #endif /* USE_REPORT_PRELUDE */
503 unfoldr f' (foldr f z xs) == (z,xs)
505 if the following holds:
507 f' (f x y) = Just (x,y)
512 unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
515 Just (a,new_b) -> a : unfoldr f new_b