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