data Set a empty :: Set a singleton :: a -> Set a insert :: a -> Set a -> Set a union, intersection, difference :: Set a -> Set a -> Set a complement :: Set a -> Set a member :: a -> Set a -> Bool toList :: Set a -> [a]
Eq a
Ord a
data Set a = Set [a]
data Set a = Empty | Node a (Set a) (Set a)
data Set a = Set (a->Bool)
data Set a = Set [a] -- Invariant: no duplicates
empty = Set []
singleton x = Set [x]
insert x (Set xs) | x `elem` xs = Set xs
| otherwise = Set (x:xs)
member x (Set xs) = x `elem` xs
union (Set xs) (Set ys) = Set (xs++[y|y<-ys,y `notElem` xs]) intersection (Set xs) (Set ys) = Set [x|x<-xs,x `elem` ys] difference (Set xs) (Set ys) = Set [x|x<-xs,x `notElem` ys]
Eq
complement
data Set a = Empty | Node a (Set a) (Set a) -- Invariant: smaller elements to the left, -- bigger to the right empty = Empty singleton x = Node x Empty Empty insert x Empty = singleton x insert x (Node y l r) | x==y = Node y l r | x<y = Node y (insert x l) r | x>y = Node y l (insert x y) member x Empty = False member x (Node y l r) = x==y || member x (if x<y then l else r)
Ord
complement
data Set a = Set (a->Bool)
data Set a = Set (a->Bool) empty = Set (const False) singleton x = Set (==x) insert x s = union (singleton x) s member x (Set f) = f x
union (Set f) (Set g) = Set (\x->f x || g x) intersection (Set f) (Set g) = Set (\x->f x && g x) complement (Set f) = Set (not . f) difference s1 s2 = intersection s1 (complement s2)
complement
toList
type Region = Point -> Bool type Point = (Double,Double)
It’s really never been an easier and more exciting time to be programming professionally in the world’s most advanced (yet usable) statically typed language.