40d115386547c3c398a01358be9eeb485024e124
[ghc-hetmet.git] / ghc / lib / required / List.hs
1 module List ( 
2     delete, deleteBy, (\\), deleteFirsts, deleteFirstsBy,
3     elemBy, notElemBy, lookupBy, maximumBy, minimumBy,
4     nub, nubBy, partition, sums, products, transpose,
5     zip4, zip5, zip6, zip7,
6     zipWith4, zipWith5, zipWith6, zipWith7,
7     unzip4, unzip5, unzip6, unzip7,
8     genericLength, genericDrop, genericTake, genericSplitAt,
9     genericReplicate,
10     elemIndex, elemIndexBy, intersperse, group, groupBy,
11     mapAccumL, mapAccumR,
12     inits, tails, subsequences, permutations, 
13     union, intersect ) where
14
15 -- delete x removes the first occurrence of x from its list argument.
16 delete                  :: (Eq a) => a -> [a] -> [a]
17 delete                  =  deleteBy (==)
18
19 deleteBy                :: (a -> a -> Bool) -> a -> [a] -> [a]
20 deleteBy eq x []        = []
21 deleteBy eq x (y:ys)    = if x `eq` y then ys else y : deleteBy eq x ys
22
23 -- list difference (non-associative).  In the result of xs \\ ys,
24 -- the first occurrence of each element of ys in turn (if any)
25 -- has been removed from xs.  Thus, (xs ++ ys) \\ xs == ys.
26 (\\)                    :: (Eq a) => [a] -> [a] -> [a]
27 (\\)                    =  foldl (flip delete)
28
29 -- Alternate name for \\
30 deleteFirsts            :: (Eq a) => [a] -> [a] -> [a]
31 deleteFirsts            = (\\)
32
33 deleteFirstsBy          :: (a -> a -> Bool) -> [a] -> [a] -> [a]
34 deleteFirstsBy eq       =  foldl (flip (deleteBy eq))
35
36 -- elem, notElem, lookup, maximumBy and minimumBy are in PreludeList
37 elemBy, notElemBy       :: (a -> a -> Bool) -> a -> [a] -> Bool
38 elemBy eq _ []          =  False
39 elemBy eq x (y:ys)      =  x `eq` y || elemBy eq x ys
40
41 notElemBy eq x xs       =  not (elemBy eq x xs)
42
43 lookupBy                :: (a -> a -> Bool) -> a -> [(a, b)] -> Maybe b
44 lookupBy eq key []      =  Nothing
45 lookupBy eq key ((x,y):xys)
46     | key `eq` x        =  Just y
47     | otherwise         =  lookupBy eq key xys
48
49 maximumBy               :: (a -> a -> a) -> [a] -> a
50 maximumBy max []        =  error "List.maximumBy: empty list"
51 maximumBy max xs        =  foldl1 max xs
52
53 minimumBy               :: (a -> a -> a) -> [a] -> a
54 minimumBy min []        =  error "List.minimumBy: empty list"
55 minimumBy min xs        =  foldl1 min xs
56
57 -- nub (meaning "essence") remove duplicate elements from its list argument.
58 nub                     :: (Eq a) => [a] -> [a]
59 nub                     =  nubBy (==)
60
61 nubBy                   :: (a -> a -> Bool) -> [a] -> [a]
62 nubBy eq []             =  []
63 nubBy eq (x:xs)         =  x : nubBy eq (filter (\ y -> not (eq x y)) xs)
64
65 -- partition takes a predicate and a list and returns a pair of lists:
66 -- those elements of the argument list that do and do not satisfy the
67 -- predicate, respectively; i,e,,
68 -- partition p xs == (filter p xs, filter (not . p) xs).
69 partition               :: (a -> Bool) -> [a] -> ([a],[a])
70 partition p xs          =  foldr select ([],[]) xs
71                            where select x (ts,fs) | p x       = (x:ts,fs)
72                                                   | otherwise = (ts, x:fs)
73
74 -- sums and products give a list of running sums or products from
75 -- a list of numbers.  e.g., sums [1,2,3] == [0,1,3,6]
76 sums, products          :: (Num a) => [a] -> [a]
77 sums                    =  scanl (+) 0 
78 products                =  scanl (*) 1 
79
80 transpose               :: [[a]] -> [[a]]
81 transpose               =  foldr
82                              (\xs xss -> zipWith (:) xs (xss ++ repeat []))
83                              []
84
85 zip4                    :: [a] -> [b] -> [c] -> [d] -> [(a,b,c,d)]
86 zip4                    =  zipWith4 (,,,)
87
88 zip5                    :: [a] -> [b] -> [c] -> [d] -> [e] -> [(a,b,c,d,e)]
89 zip5                    =  zipWith5 (,,,,)
90
91 zip6                    :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> 
92                               [(a,b,c,d,e,f)]
93 zip6                    =  zipWith6 (,,,,,)
94
95 zip7                    :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] ->
96                               [g] -> [(a,b,c,d,e,f,g)]
97 zip7                    =  zipWith7 (,,,,,,)
98
99 zipWith4                :: (a->b->c->d->e) -> [a]->[b]->[c]->[d]->[e]
100 zipWith4 z (a:as) (b:bs) (c:cs) (d:ds)
101                         =  z a b c d : zipWith4 z as bs cs ds
102 zipWith4 _ _ _ _ _      =  []
103
104 zipWith5                :: (a->b->c->d->e->f) -> 
105                            [a]->[b]->[c]->[d]->[e]->[f]
106 zipWith5 z (a:as) (b:bs) (c:cs) (d:ds) (e:es)
107                         =  z a b c d e : zipWith5 z as bs cs ds es
108 zipWith5 _ _ _ _ _ _    = []
109
110 zipWith6                :: (a->b->c->d->e->f->g) ->
111                            [a]->[b]->[c]->[d]->[e]->[f]->[g]
112 zipWith6 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs)
113                         =  z a b c d e f : zipWith6 z as bs cs ds es fs
114 zipWith6 _ _ _ _ _ _ _  = []
115
116 zipWith7                :: (a->b->c->d->e->f->g->h) ->
117                            [a]->[b]->[c]->[d]->[e]->[f]->[g]->[h]
118 zipWith7 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs) (g:gs)
119                    =  z a b c d e f g : zipWith7 z as bs cs ds es fs gs
120 zipWith7 _ _ _ _ _ _ _ _ = []
121
122 unzip4                  :: [(a,b,c,d)] -> ([a],[b],[c],[d])
123 unzip4                  =  foldr (\(a,b,c,d) ~(as,bs,cs,ds) ->
124                                         (a:as,b:bs,c:cs,d:ds))
125                                  ([],[],[],[])
126
127 unzip5                  :: [(a,b,c,d,e)] -> ([a],[b],[c],[d],[e])
128 unzip5                  =  foldr (\(a,b,c,d,e) ~(as,bs,cs,ds,es) ->
129                                         (a:as,b:bs,c:cs,d:ds,e:es))
130                                  ([],[],[],[],[])
131
132 unzip6                  :: [(a,b,c,d,e,f)] -> ([a],[b],[c],[d],[e],[f])
133 unzip6                  =  foldr (\(a,b,c,d,e,f) ~(as,bs,cs,ds,es,fs) ->
134                                         (a:as,b:bs,c:cs,d:ds,e:es,f:fs))
135                                  ([],[],[],[],[],[])
136
137 unzip7          :: [(a,b,c,d,e,f,g)] -> ([a],[b],[c],[d],[e],[f],[g])
138 unzip7          =  foldr (\(a,b,c,d,e,f,g) ~(as,bs,cs,ds,es,fs,gs) ->
139                                 (a:as,b:bs,c:cs,d:ds,e:es,f:fs,g:gs))
140                          ([],[],[],[],[],[],[])
141
142 genericLength           :: (Num i) => [b] -> i
143 genericLength []        =  0
144 genericLength (_:l)     =  1 + genericLength l
145
146 genericDrop             :: (Integral i) => i -> [a] -> [a]
147 genericDrop 0 xs        =  xs
148 genericDrop _ []        =  []
149 genericDrop n (_:xs) | n > 0  =  genericDrop (n-1) xs
150 genericDrop _ _         =  error "List.genericDrop: negative argument"
151
152 genericTake             :: (Integral i) => i -> [a] -> [a]
153 genericTake 0 _         =  []
154 genericTake _ []        =  []
155 genericTake n (x:xs) | n > 0  =  x : genericTake (n-1) xs
156 genericTake _  _        =  error "List.genericTake: negative argument"
157
158 genericSplitAt          :: (Integral i) => i -> [b] -> ([b],[b])
159 genericSplitAt 0 xs     =  ([],xs)
160 genericSplitAt _ []     =  ([],[])
161 genericSplitAt n (x:xs) | n > 0  =  (x:xs',xs'') where
162                                (xs',xs'') = genericSplitAt (n-1) xs
163 genericSplitAt _ _      =  error "List.genericSplitAt: negative argument"
164
165 genericReplicate        :: (Integral i) => i -> a -> [a]
166 genericReplicate n x    =  genericTake n (repeat x)
167
168 -- l !! (elemIndex l x) == x  if x `elem` l
169 elemIndex               :: Eq a => [a] -> a -> Int
170 elemIndex               =  elemIndexBy (==)
171
172 elemIndexBy             :: (a -> a -> Bool) -> [a] -> a -> Int
173 elemIndexBy eq [] x      = error "List.elemIndexBy: empty list"
174 elemIndexBy eq (x:xs) x' = if x `eq` x' then 0 else 1 + elemIndexBy eq xs x'
175
176 -- group splits its list argument into a list of lists of equal, adjacent
177 -- elements.  e.g.,
178 -- group "Mississippi" == ["M","i","ss","i","ss","i","pp","i"]
179 group                   :: (Eq a) => [a] -> [[a]]
180 group                   =  groupBy (==)
181
182 groupBy                 :: (a -> a -> Bool) -> [a] -> [[a]]
183 groupBy eq []           =  []
184 groupBy eq (x:xs)       =  (x:ys) : groupBy eq zs
185                            where (ys,zs) = span (eq x) xs
186                            
187
188 mapAccumL               :: (a -> b -> (a, c)) -> a -> [b] -> (a, [c])
189 mapAccumL f s []        =  (s, [])
190 mapAccumL f s (x:xs)    =  (s'',y:ys)
191                            where (s', y ) = f s x
192                                  (s'',ys) = mapAccumL f s' xs
193
194 mapAccumR               :: (a -> b -> (a, c)) -> a -> [b] -> (a, [c])
195 mapAccumR f s []        =  (s, [])
196 mapAccumR f s (x:xs)    =  (s'', y:ys)
197                            where (s'',y ) = f s' x
198                                  (s', ys) = mapAccumR f s xs
199
200 -- intersperse sep inserts sep between the elements of its list argument.
201 -- e.g. intersperse ',' "abcde" == "a,b,c,d,e"
202 intersperse             :: a -> [a] -> [a]
203 intersperse sep []      =  []
204 intersperse sep [x]     =  [x]
205 intersperse sep (x:xs)  =  x : sep : intersperse sep xs
206
207 -- inits xs returns the list of initial segments of xs, shortest first.
208 -- e.g., inits "abc" == ["","a","ab","abc"]
209 inits                   :: [a] -> [[a]]
210 inits []                =  [[]]
211 inits (x:xs)            =  [[]] ++ map (x:) (inits xs)
212
213 -- tails xs returns the list of all final segments of xs, longest first.
214 -- e.g., tails "abc" == ["abc", "bc", "c",""]
215 tails                   :: [a] -> [[a]]
216 tails []                =  [[]]
217 tails xxs@(_:xs)        =  xxs : tails xs
218
219 -- subsequences xs returns the list of all subsequences of xs.
220 -- e.g., subsequences "abc" == ["","c","b","bc","a","ac","ab","abc"]
221 subsequences            :: [a] -> [[a]]
222 subsequences []         =  [[]]
223 subsequences (x:xs)     =  subsequences xs ++ map (x:) (subsequences xs)
224
225 -- permutations xs returns the list of all permutations of xs.
226 -- e.g., permutations "abc" == ["abc","bac","bca","acb","cab","cba"]
227 permutations            :: [a] -> [[a]]
228 permutations []         =  [[]]
229 permutations (x:xs)     =  [zs | ys <- permutations xs, zs <- interleave x ys ]
230   where interleave          :: a -> [a] -> [[a]]
231         interleave x []     =  [[x]]
232         interleave x (y:ys) =  [x:y:ys] ++ map (y:) (interleave x ys)
233
234 union                   :: (Eq a) => [a] -> [a] -> [a]
235 union xs ys             =  xs ++ (ys \\ xs)
236
237 intersect               :: (Eq a) => [a] -> [a] -> [a]
238 intersect xs ys         =  [x | x <- xs, x `elem` ys]
239
240