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