summaryrefslogtreecommitdiffstats
path: root/AufgabeFFP5.hs
diff options
context:
space:
mode:
authorMatthias Wisniowski <matthias.wisniowski@gmail.com>2012-05-06 19:22:14 +0200
committerMatthias Wisniowski <matthias.wisniowski@gmail.com>2012-05-06 19:22:14 +0200
commit6d5084dde0a659debb606e837e8edf06b4b534cf (patch)
treef94803e5a4a46443c5129551bfaa0f8fee35f4f6 /AufgabeFFP5.hs
parent1f33246d13fdc722c17a3c5a10aed373848b78ec (diff)
downloadffp-6d5084dde0a659debb606e837e8edf06b4b534cf.tar.gz
ffp-6d5084dde0a659debb606e837e8edf06b4b534cf.tar.bz2
ffp-6d5084dde0a659debb606e837e8edf06b4b534cf.zip
Aufgabe 5, 1-3
Diffstat (limited to 'AufgabeFFP5.hs')
-rw-r--r--AufgabeFFP5.hs63
1 files changed, 56 insertions, 7 deletions
diff --git a/AufgabeFFP5.hs b/AufgabeFFP5.hs
index d84307c..c528be4 100644
--- a/AufgabeFFP5.hs
+++ b/AufgabeFFP5.hs
@@ -25,26 +25,75 @@ dynamic compute bnds = t
25 t = newTable (map (\coord -> (coord, compute t coord)) (range bnds)) 25 t = newTable (map (\coord -> (coord, compute t coord)) (range bnds))
26 26
27------------------------------------------------------------------------------- 27-------------------------------------------------------------------------------
28-- 1.
29-------------------------------------------------------------------------------
28 30
29bndsAS :: Array Int Int -> ((Int, Int), (Int, Int)) 31bndsAS :: Array Int Int -> ((Int, Int), (Int, Int))
30bndsAS a = ((l,l), (h,h)) 32bndsAS a = ((l,l), (h,h))
31 where 33 where
32 (l,h) = bounds a 34 (l,h) = bounds a
33 35
36-- fill the table. Lower half below diagonal not necessary
37-- but filled with the symmetric value
34compAS :: Array Int Int -> Table Int (Int, Int) -> (Int, Int) -> Int 38compAS :: Array Int Int -> Table Int (Int, Int) -> (Int, Int) -> Int
35compAS a t (i,j) 39compAS a t (i,j)
36 | i == j = a ! j 40 | i == j = a ! j
37 | i<j = compAS a t (j,i) 41 | i<j = compAS a t (j,i)
38 | otherwise = findTable t (i-1, j) + a!i 42 | 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 43
44-- computes table for array
45asTbl :: Array Int Int -> Table Int (Int, Int)
46asTbl a = dynamic (compAS a) (bndsAS a)
47
45-- maximum function for tables 48-- maximum function for tables
46tblMax :: Table Int (Int,Int) -> Int 49tblMax :: (Ord a, Ix b) => Table a b -> a
47tblMax (Tbl a) = maximum $ elems a 50tblMax (Tbl a) = maximum $ elems a
48 51
52-- maximum of the array's distance-sums
49mas :: Array Int Int -> Int 53mas :: Array Int Int -> Int
50mas a = tblMax $ dynamic (compAS a) (bndsAS a) 54mas a = tblMax $ asTbl a
55
56-------------------------------------------------------------------------------
57-- 2.
58-------------------------------------------------------------------------------
59
60-- all indices where the value equals to the maximum distance-sum
61amas :: Array Int Int -> [(Int, Int)]
62amas a = [(i,j) | ((i,j),v) <- (assocs array), i<=j, v>=m]
63 where
64 t@(Tbl array) = asTbl a
65 m = tblMax t
66
67-------------------------------------------------------------------------------
68-- 3.
69-------------------------------------------------------------------------------
70
71-- computes index with maximum index-difference
72maxL :: [(Int, Int)] -> (Int, Int)
73maxL [] = error "maximum of empty list"
74maxL [x] = x
75maxL (x:xs)
76 | l x >= l maxTail = x
77 | otherwise = maxTail
78 where
79 l (x,y) = y-x
80 maxTail = maxL xs
81
82-- index with maximum distance-sum and maximum index-difference
83lmas :: Array Int Int -> (Int, Int)
84lmas a = maxL $ amas a
85
86-------------------------------------------------------------------------------
87
88divideAndConquer :: (p->Bool) -> (p->s) -> (p->[p]) -> (p->[s]->s) -> p -> s
89divideAndConquer indiv solve divide combine initPb = dAC initPb
90 where
91 dAC pb
92 | indiv pb = solve pb
93 | otherwise = combine pb (map dAC (divide pb))
94
95
96-------------------------------------------------------------------------------
97-- 4.
98-------------------------------------------------------------------------------
99