diff options
Diffstat (limited to 'ws2015/FFP/blaetter/04')
-rw-r--r-- | ws2015/FFP/blaetter/04/FFP_U04_Lazy.hs | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/ws2015/FFP/blaetter/04/FFP_U04_Lazy.hs b/ws2015/FFP/blaetter/04/FFP_U04_Lazy.hs index 71c3045..8051ef9 100644 --- a/ws2015/FFP/blaetter/04/FFP_U04_Lazy.hs +++ b/ws2015/FFP/blaetter/04/FFP_U04_Lazy.hs | |||
@@ -13,10 +13,11 @@ | |||
13 | -- | 13 | -- |
14 | -- | 14 | -- |
15 | 15 | ||
16 | import Data.Map as Map | 16 | import qualified Data.Map as Map |
17 | import Data.Set as Set | 17 | import Data.Map (Map) |
18 | 18 | import qualified Data.Set as Set | |
19 | 19 | import Data.Set (Set) | |
20 | import qualified Data.List as List | ||
20 | 21 | ||
21 | ---- A4-1 Verzögerte Auswertung | 22 | ---- A4-1 Verzögerte Auswertung |
22 | -- Gegeben ist folgendes Programm: | 23 | -- Gegeben ist folgendes Programm: |
@@ -111,10 +112,24 @@ rs = <70> | |||
111 | -- HINWEIS: Folgen Sie dem nub2 Beispiel aus Folie 3-30 | 112 | -- HINWEIS: Folgen Sie dem nub2 Beispiel aus Folie 3-30 |
112 | 113 | ||
113 | 114 | ||
114 | transCl :: (Eq a) => (a -> [a]) -> [a] -> [a] | 115 | transCl :: Eq a => (a -> [a]) -> [a] -> [a] |
115 | transCl r xs = undefined -- !!! TODO !!! | 116 | transCl r xs = res |
117 | where | ||
118 | res = build xs 0 | ||
119 | |||
120 | build [] _ = [] | ||
121 | build xs n = xs' ++ build xs' (n + length xs') | ||
122 | where | ||
123 | xs' = strikeKnown n $ concatMap r xs | ||
124 | |||
125 | strikeKnown _ [] = [] | ||
126 | strikeKnown 0 xs = xs | ||
127 | strikeKnown n (x:xs) | ||
128 | | x `elem` take n res = strikeKnown n xs | ||
129 | | otherwise = x : strikeKnown n xs | ||
116 | 130 | ||
117 | -- Zum Testen: | 131 | -- Zum Testen: |
132 | rel1 :: Integer -> [Integer] | ||
118 | rel1 11 = [22] | 133 | rel1 11 = [22] |
119 | rel1 22 = [33] | 134 | rel1 22 = [33] |
120 | rel1 33 = [44] | 135 | rel1 33 = [44] |
@@ -123,10 +138,17 @@ rel1 n | |||
123 | | odd n, n>=1, n<=9 = [1,3,5,7,9] | 138 | | odd n, n>=1, n<=9 = [1,3,5,7,9] |
124 | | otherwise = [n] | 139 | | otherwise = [n] |
125 | 140 | ||
141 | rel1S :: Integer -> Set Integer | ||
142 | rel1S = Set.fromList . rel1 | ||
143 | |||
144 | rel2 :: Integer -> [Integer] | ||
126 | rel2 n | 145 | rel2 n |
127 | | even n = [n,n `div` 2] | 146 | | even n = [n,n `div` 2] |
128 | | otherwise = [3*n+1,n] | 147 | | otherwise = [3*n+1,n] |
129 | 148 | ||
149 | rel2S :: Integer -> Set Integer | ||
150 | rel2S = Set.fromList . rel2 | ||
151 | |||
130 | 152 | ||
131 | -- b) | 153 | -- b) |
132 | -- Implementieren Sie die Aufgabe noch mal ganz schnell | 154 | -- Implementieren Sie die Aufgabe noch mal ganz schnell |
@@ -134,7 +156,13 @@ rel2 n | |||
134 | -- sondern ganz bequem mit der Standardbibliothek für Data.Set | 156 | -- sondern ganz bequem mit der Standardbibliothek für Data.Set |
135 | 157 | ||
136 | transClS :: (Ord a) => (a -> Set a) -> Set a -> Set a | 158 | transClS :: (Ord a) => (a -> Set a) -> Set a -> Set a |
137 | transClS rel set = undefined -- !!! TODO !!! | 159 | transClS rel xs = build xs Set.empty |
160 | where | ||
161 | build xs known | ||
162 | | Set.null xs = Set.empty | ||
163 | | otherwise = xs' `Set.union` build xs' (xs' `Set.union` known) | ||
164 | where | ||
165 | xs' = Set.foldr Set.union Set.empty (Set.map rel xs) Set.\\ known | ||
138 | 166 | ||
139 | 167 | ||
140 | 168 | ||