Skip to content

Instantly share code, notes, and snippets.

@metaxy
Created August 30, 2010 10:47
Show Gist options
  • Save metaxy/557286 to your computer and use it in GitHub Desktop.
Save metaxy/557286 to your computer and use it in GitHub Desktop.
module Set (
Set,
empty,
insert,
delete,
--isEmpty,
--isElement,
--card,
--intersection,
--union,
--diff,
--show
) where
data Set a = Empty | S a (Set a) deriving(Eq,Show)
empty :: Set a
empty = Empty
-- insert an element in the set wenn nicht vorhanden
insert :: a -> Set a -> Set a
insert x s = S x s
-- removes an element from the set
--delete empty s = s
delete b (S x xs) = if' (b == x) (delete b xs) (S x (delete b (xs)))
delete b Empty = Empty
if' True x _ = x
if' False _ y = y
--is the set an empty set
isEmpty :: Set a -> Bool
isEmpty Empty = True
isEmpty _ = False
-- is a Element in the set
--isElement :: a -> Set a -> Bool
isElement b (S x xs) = if' (b == x) (True) (isElement b xs)
isElement _ Empty = False
-- the number of elements in the set
card :: Set a -> Int
card a = card' a 0 where
card' (S a s) x = card' s (x+1)
card' Empty x = x
--The intersection of two set
--intersection :: Set a -> Set a -> Set a
--the union of two sets
--union :: Set a -> Set a -> Set a
--the difference of two sets
--diff :: Set a -> Set a -> Set a
--show :: Set a -> String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment