summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--AufgabeFFP5.hs19
-rw-r--r--TestAufgabeFFP5.hs40
2 files changed, 58 insertions, 1 deletions
diff --git a/AufgabeFFP5.hs b/AufgabeFFP5.hs
index c528be4..3fb926c 100644
--- a/AufgabeFFP5.hs
+++ b/AufgabeFFP5.hs
@@ -97,3 +97,22 @@ divideAndConquer indiv solve divide combine initPb = dAC initPb
97-- 4. 97-- 4.
98------------------------------------------------------------------------------- 98-------------------------------------------------------------------------------
99 99
100miIndiv :: [a] -> Bool
101miIndiv a = length a <= 1
102
103miSolve :: (Ix a, Show a) => (b -> Bool) -> [(a,b)] -> [(a,b)]
104miSolve wf [(a,b)]
105 | wf b = [(a,b)]
106 | otherwise = []
107
108miDivide :: [a] -> [[a]]
109miDivide (x:xs) = [[x], xs]
110
111miCombine :: [a] -> [[a]] -> [a]
112miCombine _ [] = error "No matching index"
113miCombine a (x:xs)
114 | null x = miCombine a xs
115 | otherwise = [head x]
116
117minIndex :: (Ix a, Show a) => Array a b -> (b -> Bool) -> a
118minIndex a wf = fst $ head $ divideAndConquer miIndiv (miSolve wf) miDivide miCombine $ assocs a \ No newline at end of file
diff --git a/TestAufgabeFFP5.hs b/TestAufgabeFFP5.hs
index 256c1ea..eaace62 100644
--- a/TestAufgabeFFP5.hs
+++ b/TestAufgabeFFP5.hs
@@ -20,6 +20,11 @@ d = array (1,6) [(1, (-3)), (2, 1), (3, 10), (4, (-5)), (5, 8), (6, 7)]
20minA :: Array Int Int 20minA :: Array Int Int
21minA = array (0,0) [(0,0)] 21minA = array (0,0) [(0,0)]
22 22
23data Week = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving (Eq, Ord, Ix, Show)
24
25w :: Array Week String
26w = array (Tue, Sat) [(Wed, "work"), (Thu, "study"), (Tue, "study"), (Fri, "chill"), (Sat, "relax")]
27
23cases1 = TestLabel "mas" $ TestList [ 28cases1 = TestLabel "mas" $ TestList [
24 TestCase $ assertEqual "mas a" 29 TestCase $ assertEqual "mas a"
25 12 30 12
@@ -73,10 +78,43 @@ cases3 = TestLabel "lmas" $ TestList [
73 (0,0) 78 (0,0)
74 (lmas minA) 79 (lmas minA)
75 ] 80 ]
81
82cases4 = TestLabel "minIndex" $ TestList [
83 TestCase $ assertEqual "minIndex a (>5)"
84 4
85 (minIndex a (>5)),
86 TestCase $ assertEqual "minIndex a (<0)"
87 2
88 (minIndex a (<0)),
89 TestCase $ assertEqual "minIndex a (even)"
90 3
91 (minIndex a (even)),
92 TestCase $ assertEqual "minIndex b (odd)"
93 1
94 (minIndex b (odd)),
95 -- TestCase $ assertEqual "minIndex b (>100)"
96 -- error "No matching index"
97 -- (minIndex b (>100)),
98 TestCase $ assertEqual "minIndex w (=='relax')"
99 Sat
100 (minIndex w (=="relax")),
101 TestCase $ assertEqual "minIndex w (=='work')"
102 Wed
103 (minIndex w (=="work")),
104 TestCase $ assertEqual "minIndex w (=='chill')"
105 Fri
106 (minIndex w (=="chill")),
107 TestCase $ assertEqual "minIndex w (/='chill')"
108 Tue
109 (minIndex w (/="chill"))
110 -- TestCase $ assertEqual "minIndex w (=='swim')"
111 -- error "No matching index"
112 -- (minIndex w (=="swim"))
113 ]
76 114
77 115
78tests :: [Test] 116tests :: [Test]
79tests = [cases1, cases2, cases3] 117tests = [cases1, cases2, cases3, cases4]
80 118
81main = do 119main = do
82 forM tests $ \test -> 120 forM tests $ \test ->