2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[List]{Module @Lhar@}
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,
17 elemIndex, elemIndexBy, intersperse, group, groupBy,
19 inits, tails, subsequences, permutations,
25 %*********************************************************
27 \subsection{List functions}
29 %*********************************************************
32 -- delete x removes the first occurrence of x from its list argument.
33 delete :: (Eq a) => a -> [a] -> [a]
34 delete = deleteBy (==)
36 deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a]
38 deleteBy eq x (y:ys) = if x `eq` y then ys else y : deleteBy eq x ys
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)
46 -- Alternate name for \\
47 deleteFirsts :: (Eq a) => [a] -> [a] -> [a]
50 deleteFirstsBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
51 deleteFirstsBy eq = foldl (flip (deleteBy eq))
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
58 notElemBy eq x xs = not (elemBy eq x xs)
60 lookupBy :: (a -> a -> Bool) -> a -> [(a, b)] -> Maybe b
61 lookupBy eq key [] = Nothing
62 lookupBy eq key ((x,y):xys)
64 | otherwise = lookupBy eq key xys
66 maximumBy :: (a -> a -> a) -> [a] -> a
67 maximumBy max [] = error "List.maximumBy: empty list"
68 maximumBy max xs = foldl1 max xs
70 minimumBy :: (a -> a -> a) -> [a] -> a
71 minimumBy min [] = error "List.minimumBy: empty list"
72 minimumBy min xs = foldl1 min xs
74 -- nub (meaning "essence") remove duplicate elements from its list argument.
75 nub :: (Eq a) => [a] -> [a]
78 nubBy :: (a -> a -> Bool) -> [a] -> [a]
80 nubBy eq (x:xs) = x : nubBy eq (filter (\ y -> not (eq x y)) xs)
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)
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]
95 products = scanl (*) 1
97 transpose :: [[a]] -> [[a]]
99 (\xs xss -> zipWith (:) xs (xss ++ repeat []))
102 zip4 :: [a] -> [b] -> [c] -> [d] -> [(a,b,c,d)]
103 zip4 = zipWith4 (,,,)
105 zip5 :: [a] -> [b] -> [c] -> [d] -> [e] -> [(a,b,c,d,e)]
106 zip5 = zipWith5 (,,,,)
108 zip6 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] ->
110 zip6 = zipWith6 (,,,,,)
112 zip7 :: [a] -> [b] -> [c] -> [d] -> [e] -> [f] ->
113 [g] -> [(a,b,c,d,e,f,g)]
114 zip7 = zipWith7 (,,,,,,)
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 _ _ _ _ _ = []
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 _ _ _ _ _ _ = []
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 _ _ _ _ _ _ _ = []
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 _ _ _ _ _ _ _ _ = []
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))
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))
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))
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 ([],[],[],[],[],[],[])
159 genericLength :: (Num i) => [b] -> i
161 genericLength (_:l) = 1 + genericLength l
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"
169 genericTake :: (Integral i) => i -> [a] -> [a]
171 genericTake _ [] = []
172 genericTake n (x:xs) | n > 0 = x : genericTake (n-1) xs
173 genericTake _ _ = error "List.genericTake: negative argument"
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"
182 genericReplicate :: (Integral i) => i -> a -> [a]
183 genericReplicate n x = genericTake n (repeat x)
185 -- l !! (elemIndex l x) == x if x `elem` l
186 elemIndex :: Eq a => [a] -> a -> Int
187 elemIndex = elemIndexBy (==)
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'
193 -- group splits its list argument into a list of lists of equal, adjacent
195 -- group "Mississippi" == ["M","i","ss","i","ss","i","pp","i"]
196 group :: (Eq a) => [a] -> [[a]]
199 groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
201 groupBy eq (x:xs) = (x:ys) : groupBy eq zs
202 where (ys,zs) = span (eq x) xs
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
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
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
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]]
228 inits (x:xs) = [[]] ++ map (x:) (inits xs)
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]]
234 tails xxs@(_:xs) = xxs : tails xs
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)
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)
251 union :: (Eq a) => [a] -> [a] -> [a]
252 union xs ys = xs ++ (ys \\ xs)
254 intersect :: (Eq a) => [a] -> [a] -> [a]
255 intersect xs ys = [x | x <- xs, x `elem` ys]