diff options
| author | manuel <manuel@mausz.at> | 2012-05-15 12:00:05 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2012-05-15 12:00:05 +0200 |
| commit | 4df5a0cf24b811320f27e377dead9768aac54174 (patch) | |
| tree | 3404d994a34bd0dc208f9b42a7063f1bd80dc540 /AufgabeFFP6.hs | |
| parent | cc9fb7c09a62e1811aef5c22e3633e3bd42e2662 (diff) | |
| download | ffp-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.hs | 60 |
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 | |||
| 4 | where | 4 | where |
| 5 | 5 | ||
| 6 | import Data.Array | 6 | import Data.Array |
| 7 | import Debug.Trace | ||
| 7 | 8 | ||
| 8 | type F = Array Int Int | 9 | type F = Array Int Int |
| 9 | type Op = Int -> Int -> Int | 10 | type Op = Int -> Int -> Int |
| @@ -33,11 +34,30 @@ yield = yield_bt | |||
| 33 | 34 | ||
| 34 | -------------------------------------------------------------------------------- | 35 | -------------------------------------------------------------------------------- |
| 35 | 36 | ||
| 37 | instance Eq (Integer -> Integer -> Integer) where | ||
| 38 | (==) op1 op2 = ((op1 3 3) - (op2 3 3)) == 0 | ||
| 39 | instance Eq (Int -> Int -> Int) where | ||
| 40 | (==) op1 op2 = ((op1 3 3) - (op2 3 3)) == 0 | ||
| 41 | |||
| 42 | check_divByZero :: F -> G -> Bool | ||
| 43 | check_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 | |||
| 36 | type Node = ([Int], [Int], W, [Op]) | 53 | type Node = ([Int], [Int], W, [Op]) |
| 37 | 54 | ||
| 38 | goal_yield_bt :: Node -> Bool | 55 | goal_yield_bt :: Node -> Bool |
| 39 | goal_yield_bt (_, _, _, []) = False -- no operators | 56 | goal_yield_bt (_, _, _, []) = False -- no operators |
| 40 | goal_yield_bt (f, [], w, g) = eval (listArray (1, length f) f) (listArray (1, length g) g) == w | 57 | goal_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 | ||
| 41 | goal_yield_bt (_, _, _, _) = False | 61 | goal_yield_bt (_, _, _, _) = False |
| 42 | 62 | ||
| 43 | succ_yield_bt :: Node -> [Node] | 63 | succ_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 |
| 95 | generate :: F -> W -> (F, W, [G]) | 115 | generate :: F -> W -> (F, W, [G]) |
| 96 | generate f w = (f, w, [ array (1,n) entries | entries <- get_combinations n ] ) | 116 | generate 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) | ||
| 101 | get_combinations :: Int -> [[(Int, Op)]] | 122 | get_combinations :: Int -> [[(Int, Op)]] |
| 102 | get_combinations 1 = [[(1,(+))], [(1, (-))], [(1, (*))], [(1,(./.))]] | 123 | get_combinations 1 = [[(1,(+))], [(1,(-))], [(1,(*))], [(1,(./.))]] |
| 103 | get_combinations n = [ (up i) ++ entr | entr <- get_combinations (n-1), i <- get_combinations 1 ] | 124 | get_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 |
| 108 | transform :: (W -> (F,W,[G])) -> W -> ((F, W, [G]), [W]) | 129 | transform :: (W -> (F, W, [G])) -> W -> ((F, W, [G]), [W]) |
| 109 | transform fun w = ((f, w, g), map (eval f) g ) | 130 | transform 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 |
| 114 | filt :: (W -> ((F,W,[G]),[W]) ) -> W -> [G] | 136 | filt :: (W -> ((F, W, [G]), [W]) ) -> W -> [G] |
| 115 | filt fun w = [ g!!i | i <- [0..n], res!!i == w ] | 137 | filt 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 | ||
