module SetAsSortedList (Set, empty, insert, mem, union, diff, toList) where
import qualified Data.List as List
newtype Set a = Set { rep :: [a] }
toList :: Set a -> [a]
toList = rep
instance (Ord a, Show a) => Show (Set a) where
show s = "{" ++ List.intercalate "," (elts s) ++ "}"
where elts = map show . toList
empty :: Set a
empty = Set []
mem :: Ord a => a -> Set a -> Bool
mem x = elem x . rep
insert :: Ord a => a -> Set a -> Set a
insert x (Set ys) = Set $ insort x ys
where
insort x [] = [x]
insort x (y:ys) = case compare x y of
EQ -> y:ys
LT -> x:y:ys
GT -> y : insort x ys
union :: Ord a => Set a -> Set a -> Set a
union (Set xs) (Set ys) = Set $ merge xs ys
where
merge [] ys = ys
merge xs [] = xs
merge (x:xs) (y:ys) = case compare x y of
EQ -> merge xs (y:ys)
LT -> x : merge xs (y:ys)
GT -> y : merge (x:xs) ys
diff :: Ord a => Set a -> Set a -> Set a
diff (Set xs) (Set ys) = Set $ xs List.\\ ys