-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sets.hs
50 lines (33 loc) · 1.53 KB
/
Sets.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
module Sets where
import Data.List
{------------------------------------------------------------------------------
Definitions
-------------------------------------------------------------------------------}
newtype Set a = Set [a] deriving (Eq,Ord)
instance (Show a) => Show (Set a) where
showsPrec _ (Set s) str = showSet s str
showSet [] str = showString "{}" str
showSet (x:xs) str = showChar '{' ( shows x ( showl xs str))
where showl [] str = showChar '}' str
showl (x:xs) str = showChar ',' (shows x (showl xs str))
list2set :: Ord a => [a] -> Set a
list2set [] = Set []
list2set (x:xs) = insertSet x (list2set xs)
insertSet :: (Ord a) => a -> Set a -> Set a
insertSet x (Set s) = Set (insertList x s)
insertList x [] = [x]
insertList x ys@(y:ys') = case compare x y of
GT -> y : insertList x ys'
EQ -> ys
_ -> x : ys
{------------------------------------------------------------------------------
Implementations
-------------------------------------------------------------------------------}
setIntersection :: (Ord a) => Set a -> Set a -> Set a
setIntersection (Set xs) (Set ys) = Set (xs `intersect` ys)
setUnion :: (Ord a) => Set a -> Set a -> Set a
setUnion (Set xs) (Set ys) = list2set (xs ++ ys)
setDiffrence :: (Ord a) => Set a -> Set a -> Set a
setDiffrence (Set xs) (Set ys) = list2set (xs \\ ys)
getSetObjects :: (Ord a) => Set a -> [a]
getSetObjects (Set x) = x