summaryrefslogtreecommitdiffstats
path: root/AufgabeFFP6.hs
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2012-05-15 12:00:05 +0200
committermanuel <manuel@mausz.at>2012-05-15 12:00:05 +0200
commit4df5a0cf24b811320f27e377dead9768aac54174 (patch)
tree3404d994a34bd0dc208f9b42a7063f1bd80dc540 /AufgabeFFP6.hs
parentcc9fb7c09a62e1811aef5c22e3633e3bd42e2662 (diff)
downloadffp-4df5a0cf24b811320f27e377dead9768aac54174.tar.gz
ffp-4df5a0cf24b811320f27e377dead9768aac54174.tar.bz2
ffp-4df5a0cf24b811320f27e377dead9768aac54174.zip
add div by zero protection + some indentions fixes
Diffstat (limited to 'AufgabeFFP6.hs')
-rw-r--r--AufgabeFFP6.hs60
1 files changed, 40 insertions, 20 deletions
diff --git a/AufgabeFFP6.hs b/AufgabeFFP6.hs
index c60053d..1f2faff 100644
--- a/AufgabeFFP6.hs
+++ b/AufgabeFFP6.hs
@@ -4,6 +4,7 @@ module AufgabeFFP6
4where 4where
5 5
6import Data.Array 6import Data.Array
7import Debug.Trace
7 8
8type F = Array Int Int 9type F = Array Int Int
9type Op = Int -> Int -> Int 10type Op = Int -> Int -> Int
@@ -33,11 +34,30 @@ yield = yield_bt
33 34
34-------------------------------------------------------------------------------- 35--------------------------------------------------------------------------------
35 36
37instance Eq (Integer -> Integer -> Integer) where
38 (==) op1 op2 = ((op1 3 3) - (op2 3 3)) == 0
39instance Eq (Int -> Int -> Int) where
40 (==) op1 op2 = ((op1 3 3) - (op2 3 3)) == 0
41
42check_divByZero :: F -> G -> Bool
43check_divByZero f g = check_divByZero' (tail (elems f)) (elems g)
44 where
45 check_divByZero' _ [] = True
46 check_divByZero' [] _ = True
47 check_divByZero' (f:fs) (g:gs)
48 | f == 0 && g == (./.) = False
49 | otherwise = True && check_divByZero' fs gs
50
51--------------------------------------------------------------------------------
52
36type Node = ([Int], [Int], W, [Op]) 53type Node = ([Int], [Int], W, [Op])
37 54
38goal_yield_bt :: Node -> Bool 55goal_yield_bt :: Node -> Bool
39goal_yield_bt (_, _, _, []) = False -- no operators 56goal_yield_bt (_, _, _, []) = False -- no operators
40goal_yield_bt (f, [], w, g) = eval (listArray (1, length f) f) (listArray (1, length g) g) == w 57goal_yield_bt (f, [], w, g) = check_divByZero f' g' && eval f' g' == w
58 where
59 f' = listArray (1, length f) f
60 g' = listArray (1, length g) g
41goal_yield_bt (_, _, _, _) = False 61goal_yield_bt (_, _, _, _) = False
42 62
43succ_yield_bt :: Node -> [Node] 63succ_yield_bt :: Node -> [Node]
@@ -93,31 +113,31 @@ yield_gtf = filt . transform . generate
93 113
94-- produce [G] by using get_combinations 114-- produce [G] by using get_combinations
95generate :: F -> W -> (F, W, [G]) 115generate :: F -> W -> (F, W, [G])
96generate f w = (f, w, [ array (1,n) entries | entries <- get_combinations n ] ) 116generate f w = (f, w, [ array (1,n) entries | entries <- get_combinations n ])
97 where 117 where
98 n = (snd $ bounds f) - 1 118 n = (snd $ bounds f) - 1
99 119
100-- provide all selections of length n, work recursively and attach index for level (convenient for creating arrays) 120-- provide all selections of length n, work recursively and attach index for
121-- level (convenient for creating arrays)
101get_combinations :: Int -> [[(Int, Op)]] 122get_combinations :: Int -> [[(Int, Op)]]
102get_combinations 1 = [[(1,(+))], [(1, (-))], [(1, (*))], [(1,(./.))]] 123get_combinations 1 = [[(1,(+))], [(1,(-))], [(1,(*))], [(1,(./.))]]
103get_combinations n = [ (up i) ++ entr | entr <- get_combinations (n-1), i <- get_combinations 1 ] 124get_combinations n = [ (up i) ++ entr | entr <- get_combinations (n-1), i <- get_combinations 1 ]
104 where 125 where
105 up = map (\(num, x) -> ((num+n-1), x)) 126 up = map (\(num, x) -> ((num + n - 1), x))
106 127
107-- attaches a list of results 128-- attaches a list of results
108transform :: (W -> (F,W,[G])) -> W -> ((F, W, [G]), [W]) 129transform :: (W -> (F, W, [G])) -> W -> ((F, W, [G]), [W])
109transform fun w = ((f, w, g), map (eval f) g ) 130transform fun w = ((f, w, g'), map (eval f) g')
110 where 131 where
111 (f, w, g) = fun w 132 (f, w, g) = fun w
133 g' = filter (\g'' -> check_divByZero f g'') g
112 134
113-- take those, where the entry in the result list corresponds to 135-- take those, where the entry in the result list corresponds to
114filt :: (W -> ((F,W,[G]),[W]) ) -> W -> [G] 136filt :: (W -> ((F, W, [G]), [W]) ) -> W -> [G]
115filt fun w = [ g!!i | i <- [0..n], res!!i == w ] 137filt fun w = [ g!!i | i <- [0..n], res!!i == w ]
116 where 138 where
117 ( (f, _, g), res ) = fun w 139 ( (f, _, g), res ) = fun w
118 n = (length g) - 1 140 n = (length g) - 1
119
120
121 141
122-------------------------------------------------------------------------------- 142--------------------------------------------------------------------------------
123 143