diff options
Diffstat (limited to 'AufgabeFFP8.hs')
| -rw-r--r-- | AufgabeFFP8.hs | 161 |
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 @@ | |||
| 1 | module AufgabeFFP8 | 1 | module AufgabeFFP8 |
| 2 | where | 2 | where |
| 3 | 3 | ||
| 4 | import Data.Char | ||
| 5 | import Data.Array | 4 | import Data.Array |
| 6 | import Data.List hiding ((\\), insert, delete, sort) | 5 | import Data.List hiding ((\\)) |
| 7 | import Test.QuickCheck | 6 | import Test.QuickCheck |
| 8 | 7 | ||
| 9 | type Nat = [Int] | 8 | type Nat = Int |
| 10 | 9 | ||
| 11 | (\\) :: Eq a => [a] -> [a] -> [a] | 10 | -- Minfree basic version |
| 12 | xs \\ ys = filter (\x -> x `notElem` ys) xs | ||
| 13 | 11 | ||
| 14 | minfree_bv :: [Int] -> Int | 12 | minfree_bv :: [Int] -> Int |
| 15 | minfree_bv xs = head ([0..] \\ xs) | 13 | minfree_bv xs = head ([0..] \\ xs) |
| 16 | 14 | ||
| 17 | -- checklist | 15 | (\\) :: Eq a => [a] -> [a] -> [a] |
| 16 | xs \\ ys = filter (\x -> x `notElem` ys) xs | ||
| 17 | |||
| 18 | -- Minfree checklist | ||
| 19 | |||
| 18 | minfree_chl :: [Int] -> Int | 20 | minfree_chl :: [Int] -> Int |
| 19 | minfree_chl = search . checklist | 21 | minfree_chl = search . checklist |
| 20 | 22 | ||
| @@ -22,95 +24,97 @@ search :: Array Int Bool -> Int | |||
| 22 | search = length . takeWhile id . elems | 24 | search = length . takeWhile id . elems |
| 23 | 25 | ||
| 24 | checklist :: [Int] -> Array Int Bool | 26 | checklist :: [Int] -> Array Int Bool |
| 25 | checklist xs = accumArray (||) False (0, n) | 27 | checklist 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 | |||
| 30 | minfree_col :: [Int] -> Int | 33 | minfree_col :: [Int] -> Int |
| 31 | minfree_col = search_countlist . countlist | 34 | minfree_col = search0 . assocs . countlist |
| 32 | 35 | ||
| 33 | countlist :: [Int] -> Array Int Int | 36 | countlist :: [Int] -> Array Int Int |
| 34 | countlist xs = accumArray (+) 0 (0, n) (zip xs (repeat 1)) | 37 | countlist xs = accumArray (+) 0 (0,n) (zip xs (repeat 1)) |
| 35 | where n = safe_maximum xs | 38 | where n = length xs |
| 36 | 39 | ||
| 37 | safe_maximum :: [Int] -> Int | ||
| 38 | safe_maximum [] = 0 | ||
| 39 | safe_maximum xs = maximum xs | ||
| 40 | |||
| 41 | -- unused | ||
| 42 | sort :: [Int] -> [Int] | 40 | sort :: [Int] -> [Int] |
| 43 | sort xs = concat [replicate k x | (x, k) <- assocs ( countlist xs ) ] | 41 | sort xs = concat [replicate k x | (x,k) <- assocs $ countlist xs] |
| 44 | 42 | ||
| 45 | search_countlist :: Array Int Int -> Int | 43 | search0 :: [(Int, Int)] -> Int |
| 46 | search_countlist = length . takeWhile (/= 0) . elems | 44 | search0 [] = -1 |
| 45 | search0((i,0):_) = i | ||
| 46 | search0 (x:xs) = search0 xs | ||
| 47 | |||
| 48 | -- minfree basic daq | ||
| 47 | 49 | ||
| 48 | -- basic divide-and-conquer | ||
| 49 | minfree_b :: [Int] -> Int | 50 | minfree_b :: [Int] -> Int |
| 50 | minfree_b xs = if (null ([0..b-1] \\ us)) | 51 | minfree_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 | |||
| 58 | minfree_r :: [Int] -> Int | 60 | minfree_r :: [Int] -> Int |
| 59 | minfree_r xs = minfrom 0 xs | 61 | minfree_r = minfrom_r 0 |
| 60 | 62 | ||
| 61 | minfrom :: Int -> [Int] -> Int | 63 | minfrom_r :: Nat -> [Nat] -> Nat |
| 62 | minfrom a xs | 64 | minfrom_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 | |||
| 71 | minfree_o :: [Int] -> Int | 74 | minfree_o :: [Int] -> Int |
| 72 | minfree_o xs = minfrom_o 0 (length xs, xs) | 75 | minfree_o xs = minfrom_o 0 (length xs, xs) |
| 73 | 76 | ||
| 74 | minfrom_o :: Int -> (Int, [Int]) -> Int | 77 | minfrom_o :: Int -> (Int, [Int]) -> Int |
| 75 | minfrom_o a (n, xs) | 78 | minfrom_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 | 89 | divideAndConquer :: (p->Bool) -> (p->s) ->(p-> [p]) -> (p-> [s] -> s)-> p -> s |
| 86 | divideAndConquer :: (p -> Bool) -> (p -> s) -> (p -> [p]) -> (p -> [s] -> s) -> p -> s | ||
| 87 | divideAndConquer indiv solve divide combine initPb = dAC initPb | 90 | divideAndConquer 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 | 98 | b_indiv :: Int -> [Int] -> Bool |
| 99 | b_indiv l xs = length xs < l | ||
| 100 | |||
| 101 | b_solve :: [Int] -> [Int] | ||
| 102 | b_solve = id | ||
| 103 | |||
| 104 | b_divide :: Int -> [Int] -> [[Int]] | ||
| 105 | b_divide b xs = [us, vs] | ||
| 106 | where (us,vs) = partition (<b) xs | ||
| 107 | |||
| 108 | b_combine :: Int -> [Int] -> [[Int]] -> [Int] | ||
| 109 | b_combine b _ (us:vs:[]) = if (null ([0..b-1] \\ us)) | ||
| 110 | then ([b..] \\ vs) | ||
| 111 | else ([0..] \\ us) | ||
| 112 | |||
| 96 | minfree_bhof :: [Int] -> Int | 113 | minfree_bhof :: [Int] -> Int |
| 97 | minfree_bhof xs = divideAndConquer b_indiv b_solve b_divide b_combine (length xs, xs) | 114 | minfree_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 | |
| 99 | b_indiv :: (Int, [Int]) -> Bool | 116 | |
| 100 | b_indiv (0, _) = True -- empty list | 117 | -- minfree refined daq higher order |
| 101 | b_indiv (n, xs) = n /= length xs -- only divide on first call | ||
| 102 | |||
| 103 | b_solve :: (Int, [Int]) -> Int | ||
| 104 | b_solve (n, xs) = head $ [n..] \\ xs | ||
| 105 | |||
| 106 | b_divide :: (Int, [Int]) -> [ (Int, [Int]) ] | ||
| 107 | b_divide (n, xs) = [(0, us), (b, vs)] | ||
| 108 | where | ||
| 109 | b = 1 + (length xs) `div` 2 | ||
| 110 | (us, vs) = partition (<b) xs | ||
| 111 | |||
| 112 | b_combine :: (Int, [Int]) -> [Int] -> Int | ||
| 113 | b_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 |
| 156 | prop_allImplsEq_b :: [Int] -> Property | 160 | prop_allImplsEq_b :: [Int] -> Property |
| 157 | prop_allImplsEq_b xs = all (>=0) xs ==> all_eq $ calc_all (nub xs) | 161 | prop_allImplsEq_b xs = all (>=0) xs ==> all_eq $ calc_all (nub xs) \ No newline at end of file |
| 158 | |||
