1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
{-# LANGUAGE NoImplicitPrelude #-}
module Data.OldList where
import Compiler.Error
import Compiler.Base
import Compiler.Num
import Compiler.Enum
import Data.Bool
import Data.Eq
import Data.Maybe
import Data.Tuple
import Data.Function
import Data.Ord
import Data.Char
infixr 5 ++
[] ++ y = y
(h:t) ++ y = h:(t ++ y)
head (h:_) = h
head [] = error "Data.List.head: empty list"
last [x] = x
last (_:xs) = last xs
last [] = error "Data.List.last: empty list"
tail (_:t) = t
tail [] = error "Data.List.tail: empty list"
init [x] = []
init (x:xs) = x:(init xs)
init [] = error "Data.List.init: empty list"
uncons [] = Nothing
uncons (x:xs) = Just (x,xs)
null [] = True
null (_:_) = False
length :: [a] -> Int
length [] = 0
length (_:l) = 1 + length l
---
map f [] = []
map f (h:t) = (f h):(map f t)
reverse = foldl (flip (:)) []
--
intersperse _ [] = []
intersperse _ [x] = [x]
intersperse sep (x1:x2:xs) = x1:sep:(intersperse sep (x2:xs))
intercalate xs xss = concat (intersperse xs xss)
--transpose
subsequences [] = [[]]
subsequences (x:xs) = let ys = subsequences xs in ys ++ (map (x:) ys)
permutations [] = [[]]
permutations (x:xs) = let ys = permutations xs in map (x:) ys ++ map (++[x]) ys
---
foldl f z [] = z
foldl f z (x:xs) = foldl f (f z x) xs
foldl' f z [] = z
foldl' f z (x:xs) = let z' = (f z x) in seq z' (foldl' f z' xs)
foldl1 f (x:xs) = foldl f x xs
foldl1 _ [] = error "Data.List.foldl1: empty list"
foldr f z [] = z
foldr f z (x:xs) = (f x (foldr f z xs))
foldr1 _ [x] = x
foldr1 f (x:xs) = f x (foldr1 f xs)
foldr1 f _ = error "Data.List.foldr1: empty list"
--
concat xs = foldr (++) [] xs
concatMap f = concat . map f
and = foldr (&&) True
or = foldr (||) False
any p = or . map p
all p = and . map p
sum = foldl (+) 0
product = foldl (*) 1
maximum = foldl1 (max)
minimum = foldl1 (min)
--
scanl f z [] = [z]
scanl f z (x:xs) = z:scanl f (f z x) xs
scanl' f z [] = [z]
scanl' f z (x:xs) = let fzx = f z x in fzx `seq` (z:scanl' f fzx xs)
scanl1 f (x:xs) = scanl f x xs
scanl1 f [] = error "Applying scanl1 to empty list!"
scanr f z [] = [z]
-- scanr f z (x:xs) = ((f x y):ys) where ys@(y:_) = scanr f z xs
scanr1 f [x] = [x]
-- scanr1 f (x:xs) = (f x y):ys where ys@(y:_) = scanr1 f xs
-- scanr1 _ [] = error "Data.List.scanr1: empty list"
--
-- mapAccumL
-- mapAccumR
--
iterate f x = x:iterate f (f x)
iterate' f x = let fx = f x in fx `seq` x:iterate f fx
repeat x = xs where xs = x:xs
replicate n x = take n (repeat x)
cycle [] = error "Data.List.cycle: empty list"
cycle xs = let xs' = xs ++ xs' in xs'
--
-- unfoldr
--
take :: Int -> [a] -> [a]
take 0 x = []
take n [] = []
take n (h:t) = h:(take (n-1) t)
drop :: Int -> [a] -> [a]
drop 0 xs = xs
drop _ [] = []
drop n (_:xs) = drop (n-1) xs
splitAt n xs = (take n xs, drop n xs)
takeWhile _ [] = []
takeWhile p (x:xs)
| p x = x : takeWhile p xs
| otherwise = []
dropWhile _ [] = []
dropWhile p xs@(x:xs')
| p x = dropWhile p xs'
| otherwise = xs
-- dropWhileEnd
span _ xs@[] = (xs,xs)
span p xs@(x:xs')
| p x = let (ys,zs) = span p xs' in (x:ys, zs)
| otherwise = ([],xs)
break p = span (not . p)
-- stripPrefix
stripPrefix [] s = Just s
stripPrefix _ [] = Nothing
stripPrefix (p:ps) (s:ss) = if p /= s then Nothing else stripPrefix ps ss
-- group
group xs = groupBy (==) xs
-- inits
tails (x:xs) = (x:xs):(tails xs)
tails [] = []
--
-- isPrefixOf
isPrefixOf prefix s = case stripPrefix prefix s of Just _ -> True; Nothing -> False;
-- isSuffixOf
infix 4 `elem`
elem x = any (== x)
infix 4 `notElem`
notElem x = all (/= x)
lookup key [] = Nothing
lookup key ((k,v):kvs) = if (key == k) then Just v else lookup key kvs
--
-- find
find p = listToMaybe . filter p
-- FIXME: how to optimize the list comprehension version appropriately?
-- filter p xs = [ x | x <- xs, p x]
filter p [] = []
filter p (x:xs) = if (p x) then x:(filter p xs) else (filter p xs)
-- partition
infixr 9 !!
(!!) :: [a] -> Int -> a
(h:t) !! 0 = h
(h:t) !! i = t !! (i-1)
_ !! _ = error "Out of bounds list index!"
-- elemIndex
elemIndex key = listToMaybe . elemIndices key
-- elemIndices
elemIndices key = findIndices (key==)
-- findIndex
findIndex p = listToMaybe . findIndices p
-- findIndices
findIndices :: (a -> Bool) -> [a] -> [Int]
findIndices p xs = [i | (i,x) <- zip [0..] xs, p x]
--
zipWith z (a:as) (b:bs) = z a b : zipWith z as bs
zipWith _ _ _ = []
zipWith3 z (a:as) (b:bs) (c:cs) = z a b c : zipWith3 z as bs cs
zipWith3 _ _ _ _ = []
zip = zipWith (,)
zip3 = zipWith3 (,,)
-- zip4
-- zip5
-- zip6
-- zip7
unzip [] = ([] ,[] )
unzip (h:t) = (x:xs,y:ys) where (x,y) = h
(xs,ys) = unzip t
-- unzip3
-- unzip4
-- unzip5
-- unzip6
-- unzip7
lines "" = []
lines s = case break (== '\n') s of
(l, s') -> [l] ++ case s' of [] -> []
_:s'' -> lines s''
words s = case dropWhile isSpace s of
"" -> []
s' -> w : words s''
where (w, s'') = break isSpace s'
unlines [] = []
unlines (l:ls) = l ++ '\n' : unlines ls
unwords [] = ""
unwords ws = foldr1 (\w s -> w ++ ' ':s) ws
nub = nubBy (==)
-- delete
-- (\\)
-- union
-- intersect
-- sort
sort = sortOn id
-- sortOn
sortOn :: Ord b => (a -> b) -> [a] -> [a]
sortOn f [] = []
sortOn f (x:xs) = sortOn f small ++ (x : sortOn f large)
where small = [x' | x' <- xs, (f x') <= (f x)]
large = [x' | x' <- xs, (f x') > (f x)]
-- insert
--nubBy
nubBy eq (x:xs) = x:nubBy eq (filter (\y -> not (eq x y)) xs)
nubBy eq [] = []
-- unionBy
-- intersectBy
-- groupBy
groupBy p [] = [[]]
groupBy p (x:xs) = case groupBy p xs of
[] -> [[x]]
(ys:yss) -> case ys of [] -> error "can't happen!"
(z:zs) -> if p x z
then ((x:ys):yss)
else [x]:ys:yss
-- sortBy
-- insertBy
-- maximumBy
-- generic*
|