diff options
| -rw-r--r-- | AufgabeFFP3.hs | 27 | ||||
| -rw-r--r-- | TestAufgabeFFP3.hs | 21 |
2 files changed, 47 insertions, 1 deletions
diff --git a/AufgabeFFP3.hs b/AufgabeFFP3.hs index 67519be..459f510 100644 --- a/AufgabeFFP3.hs +++ b/AufgabeFFP3.hs | |||
| @@ -69,3 +69,30 @@ selector2 l = [ x | x <- best, getWeight x == min ] | |||
| 69 | min = minimum $ map getWeight best | 69 | min = minimum $ map getWeight best |
| 70 | best = selector l | 70 | best = selector l |
| 71 | 71 | ||
| 72 | |||
| 73 | -- pascal's triangle from exercise 1 | ||
| 74 | pd :: [[Integer]] | ||
| 75 | pd = [[1]] ++ (zipWith (\x y -> zipWith (+) ([0] ++ x) (y ++ [0])) pd pd) | ||
| 76 | |||
| 77 | -- naive | ||
| 78 | binom :: (Integer, Integer) -> Integer | ||
| 79 | binom (n, k) | ||
| 80 | | k == 0 || n == k = 1 | ||
| 81 | | otherwise = binom (n-1, k-1) + binom (n-1, k) | ||
| 82 | |||
| 83 | -- stream using pascal | ||
| 84 | binomS :: (Integer, Integer) -> Integer | ||
| 85 | binomS (n, k) = pd !! fromInteger n !! fromInteger k | ||
| 86 | |||
| 87 | -- calc table | ||
| 88 | binomMemoTable :: [[Integer]] | ||
| 89 | binomMemoTable = [ [ binomM (n, k) | k <- [0..] ] | n <- [0..] ] | ||
| 90 | |||
| 91 | -- using memoisation | ||
| 92 | -- base case or recursion formula using lookup table | ||
| 93 | binomM :: (Integer, Integer) -> Integer | ||
| 94 | binomM (n, k) | ||
| 95 | | k == 0 || n == k = 1 | ||
| 96 | | otherwise = get (n-1) (k-1) + get (n-1) (k) | ||
| 97 | where get n k = binomMemoTable !! fromInteger n !! fromInteger k | ||
| 98 | |||
diff --git a/TestAufgabeFFP3.hs b/TestAufgabeFFP3.hs index bbb5fbc..c83099c 100644 --- a/TestAufgabeFFP3.hs +++ b/TestAufgabeFFP3.hs | |||
| @@ -15,8 +15,27 @@ cases1 = TestLabel "foo" $ TestList [ | |||
| 15 | TestCase $ assertEqual "e" ((selector2 . (filter 5) . transformer . generator) [(5,13),(2,7),(2,6),(10,100)]) [([(2,7),(2,6)],4,13)] | 15 | TestCase $ assertEqual "e" ((selector2 . (filter 5) . transformer . generator) [(5,13),(2,7),(2,6),(10,100)]) [([(2,7),(2,6)],4,13)] |
| 16 | ] | 16 | ] |
| 17 | 17 | ||
| 18 | cases2 = TestLabel "bar" $ TestList [ | ||
| 19 | TestCase $ assertEqual "a" [ binomS (8, i) | i <- [0..7] ] [ binom (8, i) | i <- [0..7] ], | ||
| 20 | TestCase $ assertEqual "a" (binom (1,1)) (binomS (1,1)), | ||
| 21 | TestCase $ assertEqual "a" (binom (2,1)) (binomS (2,1)), | ||
| 22 | TestCase $ assertEqual "a" (binom (18,12)) (binomS (18,12)) | ||
| 23 | ] | ||
| 24 | |||
| 25 | cases3 = TestLabel "bar" $ TestList [ | ||
| 26 | TestCase $ assertEqual "a" [ binomM (8, i) | i <- [0..7] ] [ binom (8, i) | i <- [0..7] ], | ||
| 27 | TestCase $ assertEqual "a" (binom (1,1)) (binomM (1,1)), | ||
| 28 | TestCase $ assertEqual "a" (binom (2,1)) (binomM (2,1)), | ||
| 29 | TestCase $ assertEqual "a" (binom (18,12)) (binomM (18,12)) | ||
| 30 | ] | ||
| 31 | |||
| 32 | cases4 = TestLabel "bar" $ TestList [ | ||
| 33 | TestCase $ assertEqual "a" ( [binomS (n, k) | k <- [0..50], n <- [k..50] ] ) ( [binomM (n, k) | k <- [0..50], n <- [k..50] ] ) | ||
| 34 | ] | ||
| 35 | |||
| 36 | |||
| 18 | tests :: [Test] | 37 | tests :: [Test] |
| 19 | tests = [cases1] | 38 | tests = [cases1, cases2, cases3, cases4] |
| 20 | 39 | ||
| 21 | main = do | 40 | main = do |
| 22 | forM tests $ \test -> | 41 | forM tests $ \test -> |
