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
|
{-# LANGUAGE NoImplicitPrelude #-}
module Data.List where
import Compiler.Base
import Compiler.Num
import Data.Bool
import Data.Maybe
import Data.Tuple
import Data.Function
import Data.Ord
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 [] = 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 xs ++ (map (x:) xs)
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.0
product = foldl (*) 1.0
sumi = foldl (+) 0
producti = foldl (*) 1
maximum = foldl1 (max)
minimum = foldl1 (min)
--
scanl f z [] = [z]
scanl f z (x:xs) = let zx = z `f` x in (zx:scanl f zx xs)
scanl' f z [] = [z]
scanl' f z (x:xs) = let zx = z `f` x in zx `seq` (zx:scanl f zx xs)
scanl1 f (x:xs) = scanl f x xs
scanr f z [] = [z]
scanr f z (x:xs) = let ys = scanr f z xs in (x `f` (head ys)):ys
scanr1 f [x] = [x]
scanr1 f (x:xs) = let ys = scanr1 f xs in (x `f` (head ys)):ys
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 0 x = []
take n [] = []
take n (h:t) = h:(take (n-1) t)
drop 0 xs = xs
drop _ [] = []
drop n (_:xs) = drop (n-1) xs
splitAt n xs = (take n xs, drop n xs)
-- takeWhile
-- dropWhile
-- dropWhileEnd
-- span
-- break
-- stripPrefix
-- group
-- inits
tails (x:xs) = (x:xs):(tails xs)
tails [] = []
--
-- isPrefixOf
-- isSuffixOf
-- infix 4 `elem`
-- elem x = any (== x)
-- infix 4 `notElem`
-- notElem x = all (/= x)
-- Data.List.lookup
-- lookup key [] = Nothing
-- lookup key ((k,v):kvs) = if (key == k) then Just v else lookup key kvs
--
-- find
find _ [] = Nothing
find p (x:xs) = if (p x) then x else find p xs
-- 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 !!
(h:t) !! 0 = h
(h:t) !! i = t !! (i-1)
_ !! _ = error "Out of bounds list index!"
-- elemIndex
-- elemIndices
-- findIndex
-- findIndices
--
zipWith z (a:as) (b:bs) = z a b : zipWith z as bs
zipWith _ _ _ = []
zip = zipWith (,)
-- many zips ... 3-7
unzip [] = ([],[])
unzip [(x,y),l] = ([x:xs],[y:ys]) where z = unzip l
xs = fst z
ys = snd z
-- many unzips -- 3-7
-- lines
-- words
-- unlines
-- unwords
-- nub
-- delete
-- (\\)
-- union
-- intersect
-- sort
-- sortOn
-- insert
-- nubBy
-- unionBy
-- intersectBy
-- groupBy
-- sortBy
-- insertBy
-- maximumBy
-- generic*
|