diff options
| author | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-05-06 22:24:13 +0200 |
|---|---|---|
| committer | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-05-06 22:24:13 +0200 |
| commit | 2eb3ebc3247656c246258fd6a078fa5801ff80f3 (patch) | |
| tree | bb10da183ca12cec493a8f0403437e3579840037 | |
| parent | 6d5084dde0a659debb606e837e8edf06b4b534cf (diff) | |
| download | ffp-2eb3ebc3247656c246258fd6a078fa5801ff80f3.tar.gz ffp-2eb3ebc3247656c246258fd6a078fa5801ff80f3.tar.bz2 ffp-2eb3ebc3247656c246258fd6a078fa5801ff80f3.zip | |
Aufgabe 5, 4. Almost done, except testing errors
| -rw-r--r-- | AufgabeFFP5.hs | 19 | ||||
| -rw-r--r-- | TestAufgabeFFP5.hs | 40 |
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 | ||
| 100 | miIndiv :: [a] -> Bool | ||
| 101 | miIndiv a = length a <= 1 | ||
| 102 | |||
| 103 | miSolve :: (Ix a, Show a) => (b -> Bool) -> [(a,b)] -> [(a,b)] | ||
| 104 | miSolve wf [(a,b)] | ||
| 105 | | wf b = [(a,b)] | ||
| 106 | | otherwise = [] | ||
| 107 | |||
| 108 | miDivide :: [a] -> [[a]] | ||
| 109 | miDivide (x:xs) = [[x], xs] | ||
| 110 | |||
| 111 | miCombine :: [a] -> [[a]] -> [a] | ||
| 112 | miCombine _ [] = error "No matching index" | ||
| 113 | miCombine a (x:xs) | ||
| 114 | | null x = miCombine a xs | ||
| 115 | | otherwise = [head x] | ||
| 116 | |||
| 117 | minIndex :: (Ix a, Show a) => Array a b -> (b -> Bool) -> a | ||
| 118 | 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)] | |||
| 20 | minA :: Array Int Int | 20 | minA :: Array Int Int |
| 21 | minA = array (0,0) [(0,0)] | 21 | minA = array (0,0) [(0,0)] |
| 22 | 22 | ||
| 23 | data Week = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving (Eq, Ord, Ix, Show) | ||
| 24 | |||
| 25 | w :: Array Week String | ||
| 26 | w = array (Tue, Sat) [(Wed, "work"), (Thu, "study"), (Tue, "study"), (Fri, "chill"), (Sat, "relax")] | ||
| 27 | |||
| 23 | cases1 = TestLabel "mas" $ TestList [ | 28 | cases1 = 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 | |||
| 82 | cases4 = 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 | ||
| 78 | tests :: [Test] | 116 | tests :: [Test] |
| 79 | tests = [cases1, cases2, cases3] | 117 | tests = [cases1, cases2, cases3, cases4] |
| 80 | 118 | ||
| 81 | main = do | 119 | main = do |
| 82 | forM tests $ \test -> | 120 | forM tests $ \test -> |
