summaryrefslogtreecommitdiffstats
path: root/AufgabeFFP8.hs
diff options
context:
space:
mode:
Diffstat (limited to 'AufgabeFFP8.hs')
-rw-r--r--AufgabeFFP8.hs161
1 files changed, 82 insertions, 79 deletions
diff --git a/AufgabeFFP8.hs b/AufgabeFFP8.hs
index 9d244e6..436e6cb 100644
--- a/AufgabeFFP8.hs
+++ b/AufgabeFFP8.hs
@@ -1,20 +1,22 @@
1module AufgabeFFP8 1module AufgabeFFP8
2where 2where
3 3
4import Data.Char
5import Data.Array 4import Data.Array
6import Data.List hiding ((\\), insert, delete, sort) 5import Data.List hiding ((\\))
7import Test.QuickCheck 6import Test.QuickCheck
8 7
9type Nat = [Int] 8type Nat = Int
10 9
11(\\) :: Eq a => [a] -> [a] -> [a] 10-- Minfree basic version
12xs \\ ys = filter (\x -> x `notElem` ys) xs
13 11
14minfree_bv :: [Int] -> Int 12minfree_bv :: [Int] -> Int
15minfree_bv xs = head ([0..] \\ xs) 13minfree_bv xs = head ([0..] \\ xs)
16 14
17-- checklist 15(\\) :: Eq a => [a] -> [a] -> [a]
16xs \\ ys = filter (\x -> x `notElem` ys) xs
17
18-- Minfree checklist
19
18minfree_chl :: [Int] -> Int 20minfree_chl :: [Int] -> Int
19minfree_chl = search . checklist 21minfree_chl = search . checklist
20 22
@@ -22,95 +24,97 @@ search :: Array Int Bool -> Int
22search = length . takeWhile id . elems 24search = length . takeWhile id . elems
23 25
24checklist :: [Int] -> Array Int Bool 26checklist :: [Int] -> Array Int Bool
25checklist xs = accumArray (||) False (0, n) 27checklist xs = accumArray (||) False (0,n)
26 (zip (filter (<=n) xs) (repeat True)) 28 (zip (filter (<=n) xs) (repeat True))
27 where n = length xs 29 where n = length xs
28 30
29-- countlist 31-- minfree countlist
32
30minfree_col :: [Int] -> Int 33minfree_col :: [Int] -> Int
31minfree_col = search_countlist . countlist 34minfree_col = search0 . assocs . countlist
32 35
33countlist :: [Int] -> Array Int Int 36countlist :: [Int] -> Array Int Int
34countlist xs = accumArray (+) 0 (0, n) (zip xs (repeat 1)) 37countlist xs = accumArray (+) 0 (0,n) (zip xs (repeat 1))
35 where n = safe_maximum xs 38 where n = length xs
36 39
37safe_maximum :: [Int] -> Int
38safe_maximum [] = 0
39safe_maximum xs = maximum xs
40
41-- unused
42sort :: [Int] -> [Int] 40sort :: [Int] -> [Int]
43sort xs = concat [replicate k x | (x, k) <- assocs ( countlist xs ) ] 41sort xs = concat [replicate k x | (x,k) <- assocs $ countlist xs]
44 42
45search_countlist :: Array Int Int -> Int 43search0 :: [(Int, Int)] -> Int
46search_countlist = length . takeWhile (/= 0) . elems 44search0 [] = -1
45search0((i,0):_) = i
46search0 (x:xs) = search0 xs
47
48-- minfree basic daq
47 49
48-- basic divide-and-conquer
49minfree_b :: [Int] -> Int 50minfree_b :: [Int] -> Int
50minfree_b xs = if (null ([0..b-1] \\ us)) 51minfree_b xs = if (null ([0..b-1] \\ us))
51 then (head ([b..] \\ vs)) 52 then (head ([b..] \\ vs))
52 else (head ([0..] \\ us)) 53 else (head ([0..] \\ us))
53 where 54 where
54 (us, vs) = partition (<b) xs 55 b = 1 + (length xs) `div` 2
55 b = 1 + (length xs) `div` 2 56 (us, vs) = partition (<b) xs
56 57
57-- refined divide-and-conquer 58-- minfree refined daq
59
58minfree_r :: [Int] -> Int 60minfree_r :: [Int] -> Int
59minfree_r xs = minfrom 0 xs 61minfree_r = minfrom_r 0
60 62
61minfrom :: Int -> [Int] -> Int 63minfrom_r :: Nat -> [Nat] -> Nat
62minfrom a xs 64minfrom_r a xs
63 | null xs = a 65 | null xs = a
64 | length us == b-a = minfrom b vs 66 | length us == b-a = minfrom_r b vs
65 | otherwise = minfrom a us 67 | otherwise = minfrom_r b us
66 where 68 where
67 (us, vs) = partition (<b) xs 69 b = a + 1 + (length xs) `div` 2
68 b = a + 1 + (length xs) `div` 2 70 (us, vs) = partition (<b) xs
69 71
70-- optimised divide-and-conquer 72-- minfree optimized daq (p262)
73
71minfree_o :: [Int] -> Int 74minfree_o :: [Int] -> Int
72minfree_o xs = minfrom_o 0 (length xs, xs) 75minfree_o xs = minfrom_o 0 (length xs, xs)
73 76
74minfrom_o :: Int -> (Int, [Int]) -> Int 77minfrom_o :: Int -> (Int, [Int]) -> Int
75minfrom_o a (n, xs) 78minfrom_o a (n, xs)
76 | n == 0 = a 79 | n == 0 = a
77 | m == b-a = minfrom_o b (n-m, vs) 80 | m == b-a = minfrom_o b (n-m, vs)
78 | otherwise = minfrom_o a (m, us) 81 | otherwise = minfrom_o a (m, us)
79 where 82 where
80 (us, vs) = partition (<b) xs 83 (us,vs) = partition (<b) xs
81 b = a + 1 + n `div` 2 84 b = a + 1 + n `div` 2
82 m = length us 85 m = length us
83 86
87-- true daq implementations
84 88
85-- from slide 154 89divideAndConquer :: (p->Bool) -> (p->s) ->(p-> [p]) -> (p-> [s] -> s)-> p -> s
86divideAndConquer :: (p -> Bool) -> (p -> s) -> (p -> [p]) -> (p -> [s] -> s) -> p -> s
87divideAndConquer indiv solve divide combine initPb = dAC initPb 90divideAndConquer indiv solve divide combine initPb = dAC initPb
88 where 91 where
89 dAC pb 92 dAC pb
90 | indiv pb = solve pb 93 | indiv pb = solve pb
91 | otherwise = combine pb (map dAC (divide pb)) 94 | otherwise = combine pb (map dAC (divide pb))
92 95
93 96-- minfree basic daq higher order
94 97
95-- basic divide-and-conquer mittels higher order function 98b_indiv :: Int -> [Int] -> Bool
99b_indiv l xs = length xs < l
100
101b_solve :: [Int] -> [Int]
102b_solve = id
103
104b_divide :: Int -> [Int] -> [[Int]]
105b_divide b xs = [us, vs]
106 where (us,vs) = partition (<b) xs
107
108b_combine :: Int -> [Int] -> [[Int]] -> [Int]
109b_combine b _ (us:vs:[]) = if (null ([0..b-1] \\ us))
110 then ([b..] \\ vs)
111 else ([0..] \\ us)
112
96minfree_bhof :: [Int] -> Int 113minfree_bhof :: [Int] -> Int
97minfree_bhof xs = divideAndConquer b_indiv b_solve b_divide b_combine (length xs, xs) 114minfree_bhof xs = head $ divideAndConquer (b_indiv (length xs)) b_solve (b_divide b) (b_combine b) xs
98 115 where b = 1+(length xs) `div` 2
99b_indiv :: (Int, [Int]) -> Bool 116
100b_indiv (0, _) = True -- empty list 117-- minfree refined daq higher order
101b_indiv (n, xs) = n /= length xs -- only divide on first call
102
103b_solve :: (Int, [Int]) -> Int
104b_solve (n, xs) = head $ [n..] \\ xs
105
106b_divide :: (Int, [Int]) -> [ (Int, [Int]) ]
107b_divide (n, xs) = [(0, us), (b, vs)]
108 where
109 b = 1 + (length xs) `div` 2
110 (us, vs) = partition (<b) xs
111
112b_combine :: (Int, [Int]) -> [Int] -> Int
113b_combine xs sols = head sols
114 118
115 119
116-- refined divide-and-conquer mittels higher order function 120-- refined divide-and-conquer mittels higher order function
@@ -154,5 +158,4 @@ prop_allImplsEq_a xs = all_eq $ calc_all (nub xs)
154 158
155-- keine negativen listenelemented durch vorbedingung entfernt 159-- keine negativen listenelemented durch vorbedingung entfernt
156prop_allImplsEq_b :: [Int] -> Property 160prop_allImplsEq_b :: [Int] -> Property
157prop_allImplsEq_b xs = all (>=0) xs ==> all_eq $ calc_all (nub xs) 161prop_allImplsEq_b xs = all (>=0) xs ==> all_eq $ calc_all (nub xs) \ No newline at end of file
158