Last active
June 13, 2019 05:15
-
-
Save Nimrod0901/4cb46c7ad8291c2581a1e8ff8edaeb44 to your computer and use it in GitHub Desktop.
Simple Set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
Simple version of set data structure. | |
Inspired by CSE130 sp18 final. | |
-} | |
data Set a = Set (a -> Bool) | |
showSet :: (Show a) => Set a -> String | |
showSet _ = "Actually you can't show this because it you will never know what contains here" | |
instance Show a => Show (Set a) where | |
show t = showSet t | |
empty :: Set a | |
empty = Set (\_ -> False) | |
has :: Set a -> a -> Bool | |
has (Set s) n = s n | |
insert :: Eq a => Set a -> a -> Set a | |
insert s n = if (has s n) then s else Set (\x -> (x == n) || (has s x)) -- prefer this personally | |
-- insert s n = Set (\x -> (x == n) || (has s x)) | |
remove :: Eq a => Set a -> a -> Set a | |
remove s n = if not (has s n) then s else Set (\x -> (x /= n) && (has s x)) -- prefer this personally | |
-- remove s n = Set (\x -> (x /= n) && (has s x)) | |
intersect :: Eq a => Set a -> Set a -> Set a | |
intersect s1 s2 = Set (\x -> (has s1 x) && (has s2 x)) | |
union :: Eq a => Set a -> Set a -> Set a | |
union s1 s2 = Set (\x -> (has s1 x) || (has s2 x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment