changeset 36:89feab98266f

week 14 tutorial
author Markus Kaiser <markus.kaiser@in.tum.de>
date Tue, 05 Feb 2013 22:12:07 +0100
parents 782323a762a2
children 15e527f4b92e
files exercises/src/Exercise_14.hs
diffstat 1 files changed, 81 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/exercises/src/Exercise_14.hs	Wed Jan 30 22:08:04 2013 +0100
+++ b/exercises/src/Exercise_14.hs	Tue Feb 05 22:12:07 2013 +0100
@@ -1,5 +1,6 @@
 module Exercise_14 where
 import qualified Data.List
+import Test.QuickCheck
 
 
 {---------------------------------------------------------------------}
@@ -9,25 +10,99 @@
 delete = Data.List.delete {- falsche Implementierung -}
 
 
+-- Reimplementierung: Vollständig?
+prop_reimplement x xs =
+        delete x xs == filter (/=x) xs
 
-{---------------------------------------------------------------------}
-{- Aufgabe G14.1 -}
+-- Induktive Tests: Vollständig
+prop_nil :: Int -> Bool
+prop_nil x = null (delete x [])
+
+prop_cons :: Char -> Char -> [Char] -> Bool
+prop_cons x y xs =
+        delete x (y : xs) ==
+                if x == y then delete x xs
+                else y : delete x xs
+
+-- Kombinatorische Tests: Vollständig
+prop_single x y =
+        delete x [y] ==
+                if x == y then []
+                else [y]
 
-concat'' :: [[a]] -> [a]
-concat'' = undefined
+prop_append x xs ys =
+        delete x (xs ++ ys) == delete x xs ++ delete x ys
+
+-- Spezifische Tests: Unvollständig
+prop_noelem x xs = x `notElem` delete x xs
+
+prop_neutrality x xs = x `notElem` xs ==> xs == delete x xs
+
+prop_nothingNew x xs = delete x xs `subseq` xs
+        where
+        subseq = undefined
+
+prop_length x xs =
+        length xs - length [c | c <- xs, c == x] == length (delete x xs)
+
+prop_idem x xs = d == delete x d
+        where d = delete x xs
+
+-- Beispiele: Sehr fragwürdig
+prop_example x = delete x (replicate 10 x) == []
 
 
 
 {---------------------------------------------------------------------}
-{- Aufgabe G14.1 -}
+{- Aufgabe G14.2 -}
+
+concat' :: [[a]] -> [a]
+concat' [] = []
+concat' (xs:xss) = xs ++ concat' xss
+
+
+concat'' :: [[a]] -> [a]
+concat'' xss = go [] (reverse xss)
+    where
+    go acc [] = acc
+    go acc (xs:xss) = go (xs ++ acc) xss
+
+
+concat''' :: [[a]] -> [a]
+concat''' = foldr (++) []
+
+
+
+{---------------------------------------------------------------------}
+{- Aufgabe G14.3 -}
 
 {-
 
 map (*2) (1 : threes) !! 1
+= ((*2) 1 : map (*2) threes) !! 1
+= map (*2) threes !! 0
+= map (*2) (3: threes) !! 0
+= ((*2) 3 : map (*2) threes) !! 0
+= (*2) 3
+= 3 * 2
+= 6
 
-(\f -> \x -> x + f 2) (\y -> y * 2) (3 + 1)
+
+((\f -> \x -> x + f 2) (\y -> y * 2)) (3 + 1)
+= (\x -> x + (\y -> y * 2) 2) (3 + 1)
+= (3 + 1) + (\y -> y * 2) 2
+= 4 + (\y -> y * 2) 2
+= 4 + 2 * 2
+= 4 + 4
+= 8
+
 
 filter (/=3) threes
+= filter (/=3) (3 : threes)
+= filter (/=3) threes
+--> terminiert nicht
+
+threes = 3 : threes
 
 -}