view exercises/src/Exercise_14.hs @ 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
line wrap: on
line source

module Exercise_14 where
import qualified Data.List
import Test.QuickCheck


{---------------------------------------------------------------------}
{- Aufgabe G14.1 -}

delete :: Eq a => a -> [a] -> [a]
delete = Data.List.delete {- falsche Implementierung -}


-- Reimplementierung: Vollständig?
prop_reimplement x xs =
        delete x xs == filter (/=x) xs

-- 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]

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.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)
= (\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

-}



{---------------------------------------------------------------------}
{- Aufgabe H14.1 -}

{- Type me! -}



{---------------------------------------------------------------------}
{- Aufgabe H14.2 -}

{- Proof me! -}



{---------------------------------------------------------------------}
{- Aufgabe H14.3 -}

filterMap :: (a -> Maybe b) -> [a] -> [b]
filterMap = undefined