summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--AufgabeFFP6.hs4
-rw-r--r--TestAufgabeFFP6.hs24
2 files changed, 27 insertions, 1 deletions
diff --git a/AufgabeFFP6.hs b/AufgabeFFP6.hs
index 1fde756..c60053d 100644
--- a/AufgabeFFP6.hs
+++ b/AufgabeFFP6.hs
@@ -91,22 +91,26 @@ yield_bt f w = map (\(_, _, _, g) -> listArray (1, length g) g) nodes
91yield_gtf :: F -> W -> [G] 91yield_gtf :: F -> W -> [G]
92yield_gtf = filt . transform . generate 92yield_gtf = filt . transform . generate
93 93
94-- produce [G] by using get_combinations
94generate :: F -> W -> (F, W, [G]) 95generate :: F -> W -> (F, W, [G])
95generate f w = (f, w, [ array (1,n) entries | entries <- get_combinations n ] ) 96generate f w = (f, w, [ array (1,n) entries | entries <- get_combinations n ] )
96 where 97 where
97 n = (snd $ bounds f) - 1 98 n = (snd $ bounds f) - 1
98 99
100-- provide all selections of length n, work recursively and attach index for level (convenient for creating arrays)
99get_combinations :: Int -> [[(Int, Op)]] 101get_combinations :: Int -> [[(Int, Op)]]
100get_combinations 1 = [[(1,(+))], [(1, (-))], [(1, (*))], [(1,(./.))]] 102get_combinations 1 = [[(1,(+))], [(1, (-))], [(1, (*))], [(1,(./.))]]
101get_combinations n = [ (up i) ++ entr | entr <- get_combinations (n-1), i <- get_combinations 1 ] 103get_combinations n = [ (up i) ++ entr | entr <- get_combinations (n-1), i <- get_combinations 1 ]
102 where 104 where
103 up = map (\(num, x) -> ((num+n-1), x)) 105 up = map (\(num, x) -> ((num+n-1), x))
104 106
107-- attaches a list of results
105transform :: (W -> (F,W,[G])) -> W -> ((F, W, [G]), [W]) 108transform :: (W -> (F,W,[G])) -> W -> ((F, W, [G]), [W])
106transform fun w = ((f, w, g), map (eval f) g ) 109transform fun w = ((f, w, g), map (eval f) g )
107 where 110 where
108 (f, w, g) = fun w 111 (f, w, g) = fun w
109 112
113-- take those, where the entry in the result list corresponds to
110filt :: (W -> ((F,W,[G]),[W]) ) -> W -> [G] 114filt :: (W -> ((F,W,[G]),[W]) ) -> W -> [G]
111filt fun w = [ g!!i | i <- [0..n], res!!i == w ] 115filt fun w = [ g!!i | i <- [0..n], res!!i == w ]
112 where 116 where
diff --git a/TestAufgabeFFP6.hs b/TestAufgabeFFP6.hs
index 77cae92..c435045 100644
--- a/TestAufgabeFFP6.hs
+++ b/TestAufgabeFFP6.hs
@@ -22,8 +22,30 @@ cases1 = TestLabel "eval" $ TestList [
22 (eval (array (1,6) [(1,4), (2,2), (3,3), (4,-4), (5,5), (6,2)]) (array (1,5) [(1,(*)), (2,(-)), (3,(+)), (4,(./.)), (5,(+))])) 22 (eval (array (1,6) [(1,4), (2,2), (3,3), (4,-4), (5,5), (6,2)]) (array (1,5) [(1,(*)), (2,(-)), (3,(+)), (4,(./.)), (5,(+))]))
23 ] 23 ]
24 24
25instance Eq (Integer -> Integer -> Integer) where
26 (==) op1 op2 = ((op1 3 3) - (op2 3 3)) == 0
27instance Eq (Int -> Int -> Int) where
28 (==) op1 op2 = ((op1 3 3) - (op2 3 3)) == 0
29
30cases2 = TestLabel "yield_bt" $ TestList [
31 TestCase $ assertEqual "yield_bt1" (yield_bt (array (1,3) [(1,1),(2,2),(3,3)] ) 6)
32 [array (1,2) [(1,(+)),(2,(+))],array (1,2) [(1,(*)),(2,(*))]],
33 TestCase $ assertEqual "yield_bt2" (yield_bt (array (1,3) [(1,1),(2,2),(3,3)] ) 4) [],
34 TestCase $ assertEqual "yield_bt3" (yield_bt (array (1,3) [(1,1),(2,2),(3,3)]) 0)
35 [(array (1,2) [(1,(+)),(2,(-))]),(array (1,2) [(1,(*)),(2,(./.))]),(array (1,2) [(1,(./.)),(2,(*))]),(array (1,2) [(1,(./.)),(2,(./.))])]
36 ]
37
38
39cases3 = TestLabel "yield_gtf" $ TestList [
40 TestCase $ assertEqual "yield_gtf1" (yield_gtf (array (1,3) [(1,1),(2,2),(3,3)] ) 6)
41 [array (1,2) [(1,(+)),(2,(+))],array (1,2) [(1,(*)),(2,(*))]],
42 TestCase $ assertEqual "yield_gtf2" (yield_gtf (array (1,3) [(1,1),(2,2),(3,3)] ) 4) [],
43 TestCase $ assertEqual "yield_gtf3" (yield_gtf (array (1,3) [(1,1),(2,2),(3,3)]) 0)
44 [(array (1,2) [(1,(+)),(2,(-))]),(array (1,2) [(1,(*)),(2,(./.))]),(array (1,2) [(1,(./.)),(2,(*))]),(array (1,2) [(1,(./.)),(2,(./.))])]
45 ]
46
25tests :: [Test] 47tests :: [Test]
26tests = [cases1] 48tests = [cases1, cases2, cases3]
27 49
28main = do 50main = do
29 forM tests $ \test -> 51 forM tests $ \test ->