-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.hs
53 lines (39 loc) · 1.49 KB
/
Utils.hs
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
{-# OPTIONS_GHC -Wall #-}
module Utils where
space :: Int -> String
space n = replicate n ' '
split :: Eq a => a -> [a] -> [[a]]
split sep arr = foldr splitter [[]] arr
where splitter c acc | c /= sep = ((c:head acc) : tail acc)
| otherwise = [] : acc
zipNWith :: ([a] -> b) -> [[a]] -> [b]
zipNWith _ [] = []
zipNWith f xss | any null xss = []
| otherwise = f (map head xss) : zipNWith f (map tail xss)
replaceAll :: [a] -> [(Int, a)] -> [a]
replaceAll = foldr (flip replace)
replace :: [a] -> (Int, a) -> [a]
replace xs (idx, val)
| idx < 0 = error "invalid index: negative"
| otherwise = replace' xs idx val
where replace' [] _ _ = error "invalid index: greater than list length"
replace' (_:ys) 0 v = v : ys
replace' (y:ys) n v = y : replace' ys (n - 1) v
type ASSOC a b = [(a,b)]
aLookup :: (Eq a) => ASSOC a b -> a -> b -> b
aLookup [] _ def = def
aLookup ((k', v) : rest) k def | k' == k = v
| otherwise = aLookup rest k def
aHasKey :: (Eq a) => a -> ASSOC a b -> Bool
aHasKey key assoc = elem key (aDomain assoc)
aDomain :: ASSOC a b -> [a]
aDomain alist = [key | (key, _) <- alist]
aRange :: ASSOC a b -> [b]
aRange alist = [val | (_, val) <- alist]
aEmpty :: ASSOC a b
aEmpty = []
mapAccuml :: (a -> b -> (a, c)) -> a -> [b] -> (a, [c])
mapAccuml _ acc [] = (acc, [])
mapAccuml f acc (x:xs) = (acc2, x':xs')
where (acc1, x') = f acc x
(acc2, xs') = mapAccuml f acc1 xs