-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet.lhs
More file actions
133 lines (96 loc) · 4.02 KB
/
Copy pathSet.lhs
File metadata and controls
133 lines (96 loc) · 4.02 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
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
Set.lhs
ADT of sets, implemented as ordered lists without repetitions.
(c) Simon Thompson, 1995, 1998.
> module Set ( Set ,
> empty , -- Set a
> sing , -- a -> Set a
> memSet , -- Ord a => Set a -> a -> Bool
> union,inter,diff , -- Ord a => Set a -> Set a -> Set a
> eqSet , -- Eq a => Set a -> Set a -> Bool
> subSet , -- Ord a => Set a -> Set a -> Bool
> makeSet , -- Ord a => [a] -> Set a
> mapSet , -- Ord b => (a -> b) -> Set a -> Set b
> filterSet , -- (a -> Bool) -> Set a -> Set a
> foldSet , -- (a -> a -> a) -> a -> Set a -> a
> showSet , -- (a -> String) -> Set a -> String
> card -- Set a -> Int
> ) where
> import List hiding ( union )
Instance declarations for Eq and Ord
> instance Eq a => Eq (Set a) where
> (==) = eqSet
> instance Ord a => Ord (Set a) where
> (<=) = subSet
The implementation.
> newtype Set a = SetI [a]
> empty :: Set a
> empty = SetI []
> sing :: a -> Set a
> sing x = SetI [x]
> memSet :: Ord a => Set a -> a -> Bool
> memSet (SetI []) y = False
> memSet (SetI (x:xs)) y
> | x<y = memSet (SetI xs) y
> | x==y = True
> | otherwise = False
> union :: Ord a => Set a -> Set a -> Set a
> union (SetI xs) (SetI ys) = SetI (uni xs ys)
> uni :: Ord a => [a] -> [a] -> [a]
> uni [] ys = ys
> uni xs [] = xs
> uni (x:xs) (y:ys)
> | x<y = x : uni xs (y:ys)
> | x==y = x : uni xs ys
> | otherwise = y : uni (x:xs) ys
> inter :: Ord a => Set a -> Set a -> Set a
> inter (SetI xs) (SetI ys) = SetI (int xs ys)
> int :: Ord a => [a] -> [a] -> [a]
> int [] ys = []
> int xs [] = []
> int (x:xs) (y:ys)
> | x<y = int xs (y:ys)
> | x==y = x : int xs ys
> | otherwise = int (x:xs) ys
> diff :: Ord a => Set a -> Set a -> Set a
> diff (SetI xs) (SetI ys) = SetI (dif xs ys)
> dif :: Ord a => [a] -> [a] -> [a]
> dif [] ys = []
> dif xs [] = xs
> dif (x:xs) (y:ys)
> | x<y = x : dif xs (y:ys)
> | x==y = dif xs ys
> | otherwise = dif (x:xs) ys
> subSet :: Ord a => Set a -> Set a -> Bool
> subSet (SetI xs) (SetI ys) = subS xs ys
> subS :: Ord a => [a] -> [a] -> Bool
> subS [] ys = True
> subS xs [] = False
> subS (x:xs) (y:ys)
> | x<y = False
> | x==y = subS xs ys
> | x>y = subS (x:xs) ys
> eqSet :: Eq a => Set a -> Set a -> Bool
> eqSet (SetI xs) (SetI ys) = (xs == ys)
> makeSet :: Ord a => [a] -> Set a
> makeSet = SetI . remDups . sort
> where
> remDups [] = []
> remDups [x] = [x]
> remDups (x:y:xs)
> | x < y = x : remDups (y:xs)
> | otherwise = remDups (y:xs)
> mapSet :: Ord b => (a -> b) -> Set a -> Set b
> mapSet f (SetI xs) = makeSet (map f xs)
> filterSet :: (a -> Bool) -> Set a -> Set a
> filterSet p (SetI xs) = SetI (filter p xs)
> foldSet :: (a -> a -> a) -> a -> Set a -> a
> foldSet f x (SetI xs) = (foldr f x xs)
> showSet :: (a->String) -> Set a -> String
> showSet f (SetI xs) = concat (map ((++"\n") . f) xs)
> card :: Set a -> Int
> card (SetI xs) = length xs
From the exercises....
symmDiff :: Set a -> Set a -> Set a
powerSet :: Set a -> Set (Set a)
setUnion :: Set (Set a) -> Set a
setInter :: Set (Set a) -> Set a