diff options
| author | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-05-06 15:56:23 +0200 |
|---|---|---|
| committer | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-05-06 15:56:23 +0200 |
| commit | 1f33246d13fdc722c17a3c5a10aed373848b78ec (patch) | |
| tree | ae5d16f4f5c9f49013619e3bea37a5eb7e69b331 | |
| parent | 427f4787cc8adbbf10d38cc58ae329e178817976 (diff) | |
| download | ffp-1f33246d13fdc722c17a3c5a10aed373848b78ec.tar.gz ffp-1f33246d13fdc722c17a3c5a10aed373848b78ec.tar.bz2 ffp-1f33246d13fdc722c17a3c5a10aed373848b78ec.zip | |
Aufgabe 5, 1
| -rw-r--r-- | AufgabeFFP5.hs | 50 | ||||
| -rw-r--r-- | TestAufgabeFFP5.hs | 22 |
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 @@ | |||
| 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) | ||
diff --git a/TestAufgabeFFP5.hs b/TestAufgabeFFP5.hs new file mode 100644 index 0000000..03cd00b --- /dev/null +++ b/TestAufgabeFFP5.hs | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | module Main where | ||
| 2 | |||
| 3 | import Test.HUnit | ||
| 4 | import Control.Monad | ||
| 5 | import Data.Array | ||
| 6 | import AufgabeFFP5 | ||
| 7 | |||
| 8 | cases1 = 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 | |||
| 17 | tests :: [Test] | ||
| 18 | tests = [cases1] | ||
| 19 | |||
| 20 | main = do | ||
| 21 | forM tests $ \test -> | ||
| 22 | runTestTT test | ||
