Skip to content

Instantly share code, notes, and snippets.

@Nimrod0901
Last active June 13, 2019 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nimrod0901/4cb46c7ad8291c2581a1e8ff8edaeb44 to your computer and use it in GitHub Desktop.
Save Nimrod0901/4cb46c7ad8291c2581a1e8ff8edaeb44 to your computer and use it in GitHub Desktop.
Simple Set
{-
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