The Set=List functor
| import Data.List | |
| newtype Set a = | |
| Set [a] | |
| size :: | |
| Eq a => | |
| Set a | |
| -> Int | |
| size (Set a) = | |
| length (nub a) | |
| insert :: | |
| Eq a => | |
| a | |
| -> Set a | |
| -> Set a | |
| insert a (Set as) = | |
| let p = if a `elem` as | |
| then id | |
| else (a:) | |
| in Set (p as) | |
| empty :: | |
| Set a | |
| empty = | |
| Set [] | |
| instance (Eq a, Show a) => Show (Set a) where | |
| show (Set a) = | |
| show (nub a) | |
| instance Eq a => Eq (Set a) where | |
| Set [] == Set [] = | |
| True | |
| Set [] == Set (_:_) = | |
| False | |
| Set (_:_) == Set [] = | |
| False | |
| Set (a:as) == Set x = | |
| let (q, r) = break (==a) x | |
| in case r of [] -> False | |
| (_:t) -> Set as == Set t | |
| instance Functor Set where | |
| fmap f (Set a) = | |
| Set (fmap f a) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment