From 1f33246d13fdc722c17a3c5a10aed373848b78ec Mon Sep 17 00:00:00 2001 From: Matthias Wisniowski Date: Sun, 6 May 2012 15:56:23 +0200 Subject: Aufgabe 5, 1 --- AufgabeFFP5.hs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ TestAufgabeFFP5.hs | 22 ++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 AufgabeFFP5.hs create mode 100644 TestAufgabeFFP5.hs diff --git a/AufgabeFFP5.hs b/AufgabeFFP5.hs new file mode 100644 index 0000000..d84307c --- /dev/null +++ b/AufgabeFFP5.hs @@ -0,0 +1,50 @@ +module AufgabeFFP5 +where + +import Data.Array + +newtype Table a b = Tbl (Array b a) + deriving Show + +newTable :: (Ix b) => [(b, a)] -> Table a b +newTable l = Tbl (array (lo, hi) l) + where + indices = map fst l + lo = minimum indices + hi = maximum indices + +findTable :: (Ix b) => Table a b -> b -> a +findTable (Tbl a) i = a ! i + +updTable :: (Ix b) => (b, a) -> Table a b -> Table a b +updTable p@(i, x) (Tbl a) = Tbl (a // [p]) + +dynamic :: (Ix coord) => (Table entry coord -> coord -> entry) -> (coord, coord) -> (Table entry coord) +dynamic compute bnds = t + where + t = newTable (map (\coord -> (coord, compute t coord)) (range bnds)) + +------------------------------------------------------------------------------- + +bndsAS :: Array Int Int -> ((Int, Int), (Int, Int)) +bndsAS a = ((l,l), (h,h)) + where + (l,h) = bounds a + +compAS :: Array Int Int -> Table Int (Int, Int) -> (Int, Int) -> Int +compAS a t (i,j) + | i == j = a ! j + | i (Int, Int) -> Int +asDyn a (i,j) = findTable t (i,j) + where + t = dynamic (compAS a) (bndsAS a) + +-- maximum function for tables +tblMax :: Table Int (Int,Int) -> Int +tblMax (Tbl a) = maximum $ elems a + +mas :: Array Int Int -> Int +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 @@ +module Main where + +import Test.HUnit +import Control.Monad +import Data.Array +import AufgabeFFP5 + +cases1 = TestLabel "mas" $ TestList [ + TestCase $ assertEqual "exercise example" + 12 + (mas $ array (1,9) [(1,3),(2,(-5)),(3,0),(4,9),(5,2),(6,(-1)),(7,2),(8,(-5)),(9,1)]), + TestCase $ assertEqual "short list" + 21 + (mas $ array (1,6) [(1, (-3)), (2, 1), (3, 10), (4, (-5)), (5, 8), (6, 7)]) + ] + +tests :: [Test] +tests = [cases1] + +main = do + forM tests $ \test -> + runTestTT test -- cgit v1.2.3