view exercises/src/Exercise_3.hs @ 6:ae83fe6ebcd3

week 3 tutorial
author Markus Kaiser <markus.kaiser@in.tum.de>
date Wed, 07 Nov 2012 20:18:58 +0100
parents
children
line wrap: on
line source

module Exercise_3 where
import Data.List
import Data.Char (isSpace)

{- Library DO NOT CHANGE -}
type WrapFun = [Char] -> [Char]
type Picture = [[Char]]

printPicture :: Picture -> IO ()
printPicture [] = return ()
printPicture (xs : xss) = do
  putStrLn xs
  printPicture xss

pic = [".##.", ".#.#", ".###", "####"]
{- End Library -}


{---------------------------------------------------------------------}
{- Aufgabe H3.1 -}

snoc :: [a] -> a -> [a]
snoc [] y = [y]
snoc (x : xs) y = x : snoc xs y


member :: Eq a => a -> [a] -> Bool
member _ [] = False
member e (x : xs) = e == x || member e xs


butlast :: [a] -> [a]
butlast [] = []
butlast [_] = []
butlast (x : xs) = x : butlast xs


{---------------------------------------------------------------------}
{- Aufgabe G3.2 -}

uniq :: Eq a => [a] -> [a]
uniq (x:y:ys) = if x == y then uniq (y:ys) else x : uniq (y:ys)
uniq xs = xs


-- Alternativ:
uniq' :: Eq a => [a] -> [a]
uniq' [] = []
uniq' (x:xs) = f x xs
    where f x [] = [x]
          f x (y:ys) | x == y = f x ys
                     | otherwise = x : f y ys

uniqCount :: Eq a => [a] -> [(a, Integer)]
uniqCount [] = []
uniqCount (x:xs) = f (x,1) xs
    where f p [] = [p]
          f (x,c) (y:ys) | x == y = f (x, c + 1)  ys
                         | otherwise = (x,c) : f (y, 1) ys



{---------------------------------------------------------------------}
{- Aufgabe G3.3 -}

intersep :: a -> [a] -> [a]
intersep sep (c : c' : cs) = c : sep : intersep sep (c' : cs)
intersep _ cs = cs


andList :: [[Char]] -> [Char]
andList [] = ""
andList [w] = w
andList [w1, w2] = w1 ++ " and " ++ w2
andList [w1, w2, w3] = w1 ++ ", " ++ w2 ++ ", and " ++ w3
andList (w : ws) = w ++ ", " ++ andList ws



{---------------------------------------------------------------------}
{- Aufgabe G3.4 -}

triangle :: [a] -> [(a, a)]
triangle [] = []
triangle (x : xs) = [(x, x') | x' <- xs] ++ triangle xs


{- QuickCheck properties -}
prop_triangle_base = triangle ([] :: [Int]) == []
prop_triangle_one x = triangle [x] == []
prop_triangle_two x y = triangle [x, y] == [(x, y)]
prop_triangle_length xs =
  length (triangle xs) == n * (n - 1) `div` 2
  where n = length xs
prop_triangle_distinct xs =
  distinct xs ==> distinct (triangle xs)
  where distinct ys = nub ys == ys
prop_triangle_complete x xs y ys = (x, y) `elem` triangle (x : xs ++ y : ys)
prop_triangle_sound1 x y xs =
  not ((x, y) `elem` triangle (delete x (nub xs)))
  && not ((y, x) `elem` triangle (delete x (nub xs)))
prop_triangle_rec x xs =
  triangle (x : xs) == [(x, x') | x' <- xs] ++ triangle xs



{---------------------------------------------------------------------}
{- Aufgabe H3.1 -}

simplifySpaces :: [Char] -> [Char]
simplifySpaces = undefined



{---------------------------------------------------------------------}
{- Aufgabe H3.2 -}

prop_wrap1 :: WrapFun -> [Char] -> Bool
prop_wrap1 wrap xs = True

prop_wrap2 :: WrapFun -> [Char] -> Bool
prop_wrap2 wrap xs = True

prop_wrap3 :: WrapFun -> [Char] -> Bool
prop_wrap3 wrap xs = True

prop_wrap4 :: WrapFun -> [Char] -> Bool
prop_wrap4 wrap xs = True

prop_wrap5 :: WrapFun -> [Char] -> Bool
prop_wrap5 wrap xs = True

prop_wrap6 :: WrapFun -> [Char] -> Bool
prop_wrap6 wrap xs = True

prop_wrap7 :: WrapFun -> [Char] -> Bool
prop_wrap7 wrap xs = True

prop_wrap8 :: WrapFun -> [Char] -> Bool
prop_wrap8 wrap xs = True

prop_wrap9 :: WrapFun -> [Char] -> Bool
prop_wrap9 wrap xs = True

prop_wrap10 :: wrap xsFun -> [Char] -> Bool
prop_wrap10 wrap xs = True



{---------------------------------------------------------------------}
{- Aufgabe H3.3 -}

rotateClockwise :: Picture -> Picture
rotateClockwise = undefined



{---------------------------------------------------------------------}
{- Aufgabe H3.4 -}

{-WETT-}
sublist :: Eq a => [a] -> [a] -> Bool
sublist xs ys = undefined


subseq :: Eq a => [a] -> [a] -> Bool
subseq xs ys = undefined
{-TTEW-}