From 2eb3ebc3247656c246258fd6a078fa5801ff80f3 Mon Sep 17 00:00:00 2001 From: Matthias Wisniowski Date: Sun, 6 May 2012 22:24:13 +0200 Subject: Aufgabe 5, 4. Almost done, except testing errors --- AufgabeFFP5.hs | 19 +++++++++++++++++++ TestAufgabeFFP5.hs | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) 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 -- 4. ------------------------------------------------------------------------------- +miIndiv :: [a] -> Bool +miIndiv a = length a <= 1 + +miSolve :: (Ix a, Show a) => (b -> Bool) -> [(a,b)] -> [(a,b)] +miSolve wf [(a,b)] + | wf b = [(a,b)] + | otherwise = [] + +miDivide :: [a] -> [[a]] +miDivide (x:xs) = [[x], xs] + +miCombine :: [a] -> [[a]] -> [a] +miCombine _ [] = error "No matching index" +miCombine a (x:xs) + | null x = miCombine a xs + | otherwise = [head x] + +minIndex :: (Ix a, Show a) => Array a b -> (b -> Bool) -> a +minIndex 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)] minA :: Array Int Int minA = array (0,0) [(0,0)] +data Week = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving (Eq, Ord, Ix, Show) + +w :: Array Week String +w = array (Tue, Sat) [(Wed, "work"), (Thu, "study"), (Tue, "study"), (Fri, "chill"), (Sat, "relax")] + cases1 = TestLabel "mas" $ TestList [ TestCase $ assertEqual "mas a" 12 @@ -73,10 +78,43 @@ cases3 = TestLabel "lmas" $ TestList [ (0,0) (lmas minA) ] + +cases4 = TestLabel "minIndex" $ TestList [ + TestCase $ assertEqual "minIndex a (>5)" + 4 + (minIndex a (>5)), + TestCase $ assertEqual "minIndex a (<0)" + 2 + (minIndex a (<0)), + TestCase $ assertEqual "minIndex a (even)" + 3 + (minIndex a (even)), + TestCase $ assertEqual "minIndex b (odd)" + 1 + (minIndex b (odd)), + -- TestCase $ assertEqual "minIndex b (>100)" + -- error "No matching index" + -- (minIndex b (>100)), + TestCase $ assertEqual "minIndex w (=='relax')" + Sat + (minIndex w (=="relax")), + TestCase $ assertEqual "minIndex w (=='work')" + Wed + (minIndex w (=="work")), + TestCase $ assertEqual "minIndex w (=='chill')" + Fri + (minIndex w (=="chill")), + TestCase $ assertEqual "minIndex w (/='chill')" + Tue + (minIndex w (/="chill")) + -- TestCase $ assertEqual "minIndex w (=='swim')" + -- error "No matching index" + -- (minIndex w (=="swim")) + ] tests :: [Test] -tests = [cases1, cases2, cases3] +tests = [cases1, cases2, cases3, cases4] main = do forM tests $ \test -> -- cgit v1.2.3