summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Wisniowski <matthias.wisniowski@gmail.com>2012-05-06 15:56:23 +0200
committerMatthias Wisniowski <matthias.wisniowski@gmail.com>2012-05-06 15:56:23 +0200
commit1f33246d13fdc722c17a3c5a10aed373848b78ec (patch)
treeae5d16f4f5c9f49013619e3bea37a5eb7e69b331
parent427f4787cc8adbbf10d38cc58ae329e178817976 (diff)
downloadffp-1f33246d13fdc722c17a3c5a10aed373848b78ec.tar.gz
ffp-1f33246d13fdc722c17a3c5a10aed373848b78ec.tar.bz2
ffp-1f33246d13fdc722c17a3c5a10aed373848b78ec.zip
Aufgabe 5, 1
-rw-r--r--AufgabeFFP5.hs50
-rw-r--r--TestAufgabeFFP5.hs22
2 files changed, 72 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)
diff --git a/TestAufgabeFFP5.hs b/TestAufgabeFFP5.hs
new file mode 100644
index 0000000..03cd00b
--- /dev/null
+++ b/TestAufgabeFFP5.hs
@@ -0,0 +1,22 @@
1module Main where
2
3import Test.HUnit
4import Control.Monad
5import Data.Array
6import AufgabeFFP5
7
8cases1 = TestLabel "mas" $ TestList [
9 TestCase $ assertEqual "exercise example"
10 12
11 (mas $ array (1,9) [(1,3),(2,(-5)),(3,0),(4,9),(5,2),(6,(-1)),(7,2),(8,(-5)),(9,1)]),
12 TestCase $ assertEqual "short list"
13 21
14 (mas $ array (1,6) [(1, (-3)), (2, 1), (3, 10), (4, (-5)), (5, 8), (6, 7)])
15 ]
16
17tests :: [Test]
18tests = [cases1]
19
20main = do
21 forM tests $ \test ->
22 runTestTT test