diff options
Diffstat (limited to 'AufgabeFFP5.hs')
| -rw-r--r-- | AufgabeFFP5.hs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/AufgabeFFP5.hs b/AufgabeFFP5.hs new file mode 100644 index 0000000..d84307c --- /dev/null +++ b/AufgabeFFP5.hs | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | module AufgabeFFP5 | ||
| 2 | where | ||
| 3 | |||
| 4 | import Data.Array | ||
| 5 | |||
| 6 | newtype Table a b = Tbl (Array b a) | ||
| 7 | deriving Show | ||
| 8 | |||
| 9 | newTable :: (Ix b) => [(b, a)] -> Table a b | ||
| 10 | newTable l = Tbl (array (lo, hi) l) | ||
| 11 | where | ||
| 12 | indices = map fst l | ||
| 13 | lo = minimum indices | ||
| 14 | hi = maximum indices | ||
| 15 | |||
| 16 | findTable :: (Ix b) => Table a b -> b -> a | ||
| 17 | findTable (Tbl a) i = a ! i | ||
| 18 | |||
| 19 | updTable :: (Ix b) => (b, a) -> Table a b -> Table a b | ||
| 20 | updTable p@(i, x) (Tbl a) = Tbl (a // [p]) | ||
| 21 | |||
| 22 | dynamic :: (Ix coord) => (Table entry coord -> coord -> entry) -> (coord, coord) -> (Table entry coord) | ||
| 23 | dynamic compute bnds = t | ||
| 24 | where | ||
| 25 | t = newTable (map (\coord -> (coord, compute t coord)) (range bnds)) | ||
| 26 | |||
| 27 | ------------------------------------------------------------------------------- | ||
| 28 | |||
| 29 | bndsAS :: Array Int Int -> ((Int, Int), (Int, Int)) | ||
| 30 | bndsAS a = ((l,l), (h,h)) | ||
| 31 | where | ||
| 32 | (l,h) = bounds a | ||
| 33 | |||
| 34 | compAS :: Array Int Int -> Table Int (Int, Int) -> (Int, Int) -> Int | ||
| 35 | compAS a t (i,j) | ||
| 36 | | i == j = a ! j | ||
| 37 | | i<j = compAS a t (j,i) | ||
| 38 | | otherwise = findTable t (i-1, j) + a!i | ||
| 39 | |||
| 40 | asDyn :: Array Int Int -> (Int, Int) -> Int | ||
| 41 | asDyn a (i,j) = findTable t (i,j) | ||
| 42 | where | ||
| 43 | t = dynamic (compAS a) (bndsAS a) | ||
| 44 | |||
| 45 | -- maximum function for tables | ||
| 46 | tblMax :: Table Int (Int,Int) -> Int | ||
| 47 | tblMax (Tbl a) = maximum $ elems a | ||
| 48 | |||
| 49 | mas :: Array Int Int -> Int | ||
| 50 | mas a = tblMax $ dynamic (compAS a) (bndsAS a) | ||
