From: Camil Staps Date: Mon, 2 Mar 2015 12:58:22 +0000 (+0100) Subject: 6.3 X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=bee9e3d3599389dcafab90a2b0ae9ceedc30de97;p=fp1415.git 6.3 --- diff --git a/files/practicum/StdSet.icl b/files/practicum/StdSet.icl index b152f37..6cad7f1 100644 --- a/files/practicum/StdSet.icl +++ b/files/practicum/StdSet.icl @@ -1,5 +1,25 @@ -implementation module StdSet +definition module StdSet -import StdEnv +import StdClass :: Set a + +toSet :: [a] -> Set a | Eq a +fromSet :: (Set a) -> [a] + +isEmptySet :: (Set a) -> Bool +isDisjoint :: (Set a) (Set a) -> Bool | Eq a +isSubset :: (Set a) (Set a) -> Bool | Eq a +isStrictSubset :: (Set a) (Set a) -> Bool | Eq a +memberOfSet :: a (Set a) -> Bool | Eq a +union :: (Set a) (Set a) -> Set a | Eq a +intersection :: (Set a) (Set a) -> Set a | Eq a +nrOfElements :: (Set a) -> Int +without :: (Set a) (Set a) -> Set a | Eq a + +product :: (Set a) (Set b) -> Set (a,b) + +instance zero (Set a) +instance == (Set a) | Eq a + +powerSet :: (Set a) -> Set (Set a) diff --git a/week4/camil/StdSet.dcl b/week4/camil/StdSet.dcl new file mode 100644 index 0000000..6cad7f1 --- /dev/null +++ b/week4/camil/StdSet.dcl @@ -0,0 +1,25 @@ +definition module StdSet + +import StdClass + +:: Set a + +toSet :: [a] -> Set a | Eq a +fromSet :: (Set a) -> [a] + +isEmptySet :: (Set a) -> Bool +isDisjoint :: (Set a) (Set a) -> Bool | Eq a +isSubset :: (Set a) (Set a) -> Bool | Eq a +isStrictSubset :: (Set a) (Set a) -> Bool | Eq a +memberOfSet :: a (Set a) -> Bool | Eq a +union :: (Set a) (Set a) -> Set a | Eq a +intersection :: (Set a) (Set a) -> Set a | Eq a +nrOfElements :: (Set a) -> Int +without :: (Set a) (Set a) -> Set a | Eq a + +product :: (Set a) (Set b) -> Set (a,b) + +instance zero (Set a) +instance == (Set a) | Eq a + +powerSet :: (Set a) -> Set (Set a) diff --git a/week4/camil/StdSet.icl b/week4/camil/StdSet.icl new file mode 100644 index 0000000..01c854b --- /dev/null +++ b/week4/camil/StdSet.icl @@ -0,0 +1,70 @@ +implementation module StdSet + +import StdEnv +import StdClass + +:: Set a :== [a] + +toSet :: [a] -> Set a | Eq a +toSet l = toSet` l [] +where + toSet` [] s = s + toSet` [x:xs] s = toSet` xs (join x s) + where + join :: a (Set a) -> Set a | Eq a + join e s + | memberOfSet e s = s + | otherwise = s ++ [e] + +fromSet :: (Set a) -> [a] +fromSet s = s + +isEmptySet :: (Set a) -> Bool +isEmptySet [] = True +isEmptySet _ = False + +isDisjoint :: (Set a) (Set a) -> Bool | Eq a +isDisjoint s1 s2 = length (intersection s1 s2) == 0 + +isSubset :: (Set a) (Set a) -> Bool | Eq a +isSubset s1 s2 = nrOfElements (intersection s1 s2) == nrOfElements s1 + +isStrictSubset :: (Set a) (Set a) -> Bool | Eq a +isStrictSubset s1 s2 = isSubset s1 s2 && s1 <> s2 + +memberOfSet :: a (Set a) -> Bool | Eq a +memberOfSet e [] = False +memberOfSet e [x:xs] + | e == x = True + | otherwise = memberOfSet e xs + +union :: (Set a) (Set a) -> Set a | Eq a +union s1 s2 = toSet (s1 ++ s2) + +intersection :: (Set a) (Set a) -> Set a | Eq a +intersection s1 s2 = [e \\ e <- s1 | memberOfSet e s2] + +nrOfElements :: (Set a) -> Int +nrOfElements s = length (fromSet s) + +without :: (Set a) (Set a) -> Set a | Eq a +without s1 s2 = [e \\ e <- s1 | (memberOfSet e s2) == False] + +product :: (Set a) (Set b) -> Set (a,b) +product s1 s2 = [(e1,e2) \\ e1 <- s1, e2 <- s2] + +instance zero (Set a) +where zero = [] + +instance == (Set a) | Eq a +where (==) s1 s2 = isSubset s1 s2 && isSubset s2 s1 + +powerSet :: (Set a) -> Set (Set a) +powerSet [] = [zero] +powerSet [e:es] = map ((++) [e]) (powerSet es) ++ powerSet es + +powerSet` :: (Set a) -> [Int] +powerSet` s = [0 .. 2^(nrOfElements s) - 1] + +takeMod :: (Set a) Int -> Set a +takeMod s m = [e \\ e <- s & k <- [0..] | k rem m == 0]