Created
May 28, 2014 13:38
-
-
Save ichistmeinname/871dec7b3b2a1d798308 to your computer and use it in GitHub Desktop.
Sixth group session
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
module Set where | |
-- Menge als Funktion?! | |
type Set a = a -> Bool | |
-- empty | |
empty :: Set Int -- Int -> Bool | |
empty _ = False | |
-- empty x = const False x | |
-- empty = const False | |
-- const :: a -> b -> a | |
-- const x y = x | |
-- Set Int :: Int -> Bool | |
-- s :: Int -> Bool | |
-- v :: Int | |
-- => s v :: Bool | |
isElem :: Set Int -> Int -> Bool | |
isElem = id | |
-- isElem s = s | |
-- isElem s v = s v | |
insert :: Set Int -> Int -> Set Int | |
insert s v = s' | |
where | |
s' :: Set Int -- Int -> Bool | |
s' w | w == v = True | |
| otherwise = s w | |
remove :: Set Int -> Int -> Set Int | |
remove s v = s' | |
where | |
s' :: Set Int -- Int -> Bool | |
s' w | w == v = False | |
| otherwise = s w | |
-- Set Int -> Set Int -> (Int -> Bool) | |
-- (Int -> Bool) -> (Int -> Bool) -> (Int -> Bool) | |
union :: Set Int -> Set Int -> Set Int | |
union = lift (||) | |
-- union s1 s2 = lift (||) s1 s2 | |
-- union s1 s2 = s' | |
-- where | |
-- s' = s1 v || s2 v | |
intersection :: Set Int -> Set Int -> Set Int | |
intersection = lift (&&) | |
-- intersection s1 s2 = lift (&&) s1 s2 | |
-- intersection s1 s2 = s' | |
-- where | |
-- s' = s1 v && s2 v | |
-- lift :: (Bool -> Bool -> Bool) | |
-- -> Set Int | |
-- -> Set Int | |
-- -> Set Int | |
lift :: (b -> b -> b) | |
-> (a -> b) | |
-> (a -> b) | |
-> a | |
-> b | |
lift op s1 s2 v = s1 v `op` s2 v | |
complement :: Set Int -> Set Int | |
complement s v = not (s v) | |
-- complement s = not . s | |
-- compmement = (not .) | |
difference :: Set Int -> Set Int -> Set Int | |
difference s1 = intersection s1 . complement | |
-- difference s1 s2 = | |
-- intersection s1 (complement s2) | |
-- difference s1 s2 = a' | |
-- where | |
-- a' :: Int -> Bool | |
-- a' v = s1 v && not (s2 v) | |
-- listToSet :: [Int] -> Int -> Bool | |
listToSet :: [Int] -> Set Int | |
listToSet xs = s' | |
where | |
s' v = v `elem` xs | |
-- s' = (`elem` xs) | |
----- Kurze Wiederholung des Arrays | |
type Array a = Int -> a | |
emptyArray i = | |
error ("Access to non-initialized component " ++ show i) | |
getIndex :: Array a -> Int -> a | |
-- Array a :: Int -> a | |
-- arr :: Array a, i :: Int | |
-- arr i :: a | |
getIndex = id | |
-- getIndex a = a | |
-- getIndex a i = a i | |
-- (Int -> a) -> Int -> a -> (Int -> a) | |
putIndex :: Array a -> Int -> a -> Array a | |
putIndex a i v = a' | |
where | |
a' j | i == j = v | |
| otherwise = a j | |
removeIndex :: Array a -> Int -> Array a | |
removeIndex a i = a' | |
where | |
a' j | i == j = emptyArray j | |
| otherwise = a j |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment