-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistkit.hs
More file actions
88 lines (58 loc) · 2.28 KB
/
listkit.hs
File metadata and controls
88 lines (58 loc) · 2.28 KB
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
module ListKit where
import List
{- List utilities -}
isNull [] = True
isNull _ = False
unassoc a = filter ((/= a) . fst)
nubassoc [] = []
nubassoc ((x,y):xys) = (x,y) : (nubassoc $ unassoc x xys)
graph f xs = [(x, f x) | x <- xs]
alistmap f = map (\(a, b) -> (a, f b))
bagEq a b = a \\ b == [] && b \\ a == []
setEq a b = (nub a) `bagEq` (nub b)
u a b = nub (a ++ b)
union lists = nub $ concat lists
contains a b = null(b \\ a)
setMinus :: Ord a => [a] -> [a] -> [a]
setMinus xs ys = nub $ sort $ xs \\ ys
(\\\) :: Ord a => [a] -> [a] -> [a]
(\\\) = setMinus
allEq [] = True
allEq (x:xs) = all (== x) xs
disjointAlist [] _ = True
disjointAlist _ [] = True
disjointAlist xs ((a,b):ys) =
(not $ any ((== a) . fst) xs) && disjointAlist xs ys
sortOn f l = sortBy (\x y -> f x `compare` f y) l
groupOn f l = groupBy (\x y -> f x == f y) l
asList Nothing = []
asList (Just x) = [x]
-- zipAlist: given two alists with the same domain,
-- returns an alist mapping each of those domain values to
-- the pair of the two corresponding values from the given lists.
zipAlist xs ys =
let xsys = zip (sortAlist xs) (sortAlist ys) in
if not $ and [x == y | ((x, a), (y, b)) <- xsys] then
error "alist mismatch in zipAlist"
else [(x, (a, b)) | ((x, a), (y, b)) <- xsys]
mapstrcat :: String -> (a -> String) -> [a] -> String
mapstrcat glue f xs = concat $ List.intersperse glue (map f xs)
-- shadow: given two alists, return the elements of the first that
-- are NOT mapped by the second
shadow as bs = [(a,x) | (a,x) <- as, a `notElem` domBs]
where domBs = map fst bs
validEnv xs = length (nub $ map fst xs) == length xs
mr agg xs = map reduceGroup (collate fst xs)
where reduceGroup xs = let (as, bs) = unzip xs in
(the as, agg bs)
the xs | allEq xs = head xs
| otherwise= error "'the' applied to nonconstant list"
onCorresponding :: Ord a => ([b]->c) -> [(a,b)] -> [c]
onCorresponding agg xs = map reduceGroup (collate fst xs)
where reduceGroup xs = agg $ map snd xs
dom alist = map fst alist
rng alist = map snd alist
collate proj = groupBy (\x y -> proj x == proj y) .
sortBy (\x y -> proj x `compare` proj y)
sortAlist :: [(String, b)] -> [(String, b)]
sortAlist = sortBy (\a b -> fst a `compare` fst b)