diff options
| author | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-05-29 22:06:00 +0200 |
|---|---|---|
| committer | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-05-29 22:06:00 +0200 |
| commit | bf712038d85069afcc8bc063f40caca466ca81b6 (patch) | |
| tree | 713854bb05dfc78ad8a454d6c0538127412a1e22 | |
| parent | 45cfb83e7a74121f8847f73fdc522f5785470733 (diff) | |
| download | ffp-bf712038d85069afcc8bc063f40caca466ca81b6.tar.gz ffp-bf712038d85069afcc8bc063f40caca466ca81b6.tar.bz2 ffp-bf712038d85069afcc8bc063f40caca466ca81b6.zip | |
Bsp 8.1
| -rw-r--r-- | AufgabeFFP8.hs | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/AufgabeFFP8.hs b/AufgabeFFP8.hs new file mode 100644 index 0000000..2747597 --- /dev/null +++ b/AufgabeFFP8.hs | |||
| @@ -0,0 +1,119 @@ | |||
| 1 | module AufgabeFFP8 | ||
| 2 | where | ||
| 3 | |||
| 4 | import Data.Array | ||
| 5 | import Data.List hiding ((\\)) | ||
| 6 | import Test.QuickCheck | ||
| 7 | |||
| 8 | type Nat = Int | ||
| 9 | |||
| 10 | -- Minfree basic version | ||
| 11 | |||
| 12 | minfree_bv :: [Int] -> Int | ||
| 13 | minfree_bv xs = head ([0..] \\ xs) | ||
| 14 | |||
| 15 | (\\) :: Eq a => [a] -> [a] -> [a] | ||
| 16 | xs \\ ys = filter (\x -> x `notElem` ys) xs | ||
| 17 | |||
| 18 | -- Minfree checklist | ||
| 19 | |||
| 20 | minfree_chl :: [Int] -> Int | ||
| 21 | minfree_chl = search . checklist | ||
| 22 | |||
| 23 | search :: Array Int Bool -> Int | ||
| 24 | search = length . takeWhile id . elems | ||
| 25 | |||
| 26 | checklist :: [Int] -> Array Int Bool | ||
| 27 | checklist xs = accumArray (||) False (0,n) | ||
| 28 | (zip (filter (<=n) xs) (repeat True)) | ||
| 29 | where n = length xs | ||
| 30 | |||
| 31 | -- minfree countlist | ||
| 32 | |||
| 33 | minfree_col :: [Int] -> Int | ||
| 34 | minfree_col = search0 . assocs . countlist | ||
| 35 | |||
| 36 | countlist :: [Int] -> Array Int Int | ||
| 37 | countlist xs = accumArray (+) 0 (0,n) (zip xs (repeat 1)) | ||
| 38 | where n = length xs | ||
| 39 | |||
| 40 | sort :: [Int] -> [Int] | ||
| 41 | sort xs = concat [replicate k x | (x,k) <- assocs $ countlist xs] | ||
| 42 | |||
| 43 | search0 :: [(Int, Int)] -> Int | ||
| 44 | search0 [] = -1 | ||
| 45 | search0((i,0):_) = i | ||
| 46 | search0 (x:xs) = search0 xs | ||
| 47 | |||
| 48 | -- minfree basic daq | ||
| 49 | |||
| 50 | minfree_b :: [Int] -> Int | ||
| 51 | minfree_b xs = if (null ([0..b-1] \\ us)) | ||
| 52 | then (head ([b..] \\ vs)) | ||
| 53 | else (head ([0..] \\ us)) | ||
| 54 | where | ||
| 55 | b = 1 + (length xs) `div` 2 | ||
| 56 | (us, vs) = partition (<b) xs | ||
| 57 | |||
| 58 | -- minfree refined daq | ||
| 59 | |||
| 60 | minfree_r :: [Int] -> Int | ||
| 61 | minfree_r = minfrom_r 0 | ||
| 62 | |||
| 63 | minfrom_r :: Nat -> [Nat] -> Nat | ||
| 64 | minfrom_r a xs | ||
| 65 | | null xs = a | ||
| 66 | | length us == b-a = minfrom_r b vs | ||
| 67 | | otherwise = minfrom_r b us | ||
| 68 | where | ||
| 69 | b = a + 1 + (length xs) `div` 2 | ||
| 70 | (us, vs) = partition (<b) xs | ||
| 71 | |||
| 72 | -- minfree optimized daq (p262) | ||
| 73 | |||
| 74 | minfree_o :: [Int] -> Int | ||
| 75 | minfree_o xs = minfrom_o 0 (length xs, xs) | ||
| 76 | |||
| 77 | minfrom_o :: Int -> (Int, [Int]) -> Int | ||
| 78 | minfrom_o a (n, xs) | ||
| 79 | | n == 0 = a | ||
| 80 | | m == b-a = minfrom_o b (n-m, vs) | ||
| 81 | | otherwise = minfrom_o a (m, us) | ||
| 82 | where | ||
| 83 | (us,vs) = partition (<b) xs | ||
| 84 | b = a + 1 + n `div` 2 | ||
| 85 | m = length us | ||
| 86 | |||
| 87 | -- true daq implementations | ||
| 88 | |||
| 89 | divideAndConquer :: (p->Bool) -> (p->s) ->(p-> [p]) -> (p-> [s] -> s)-> p -> s | ||
| 90 | divideAndConquer indiv solve divide combine initPb = dAC initPb | ||
| 91 | where | ||
| 92 | dAC pb | ||
| 93 | | indiv pb = solve pb | ||
| 94 | | otherwise = combine pb (map dAC (divide pb)) | ||
| 95 | |||
| 96 | -- minfree basic daq higher order | ||
| 97 | |||
| 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 | |||
| 113 | minfree_bhof :: [Int] -> Int | ||
| 114 | minfree_bhof xs = head $ divideAndConquer (b_indiv (length xs)) b_solve (b_divide b) (b_combine b) xs | ||
| 115 | where b = 1+(length xs) `div` 2 | ||
| 116 | |||
| 117 | -- minfree refined daq higher order | ||
| 118 | |||
| 119 | |||
