comparison 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
comparison
equal deleted inserted replaced
5:f1a337c9f260 6:ae83fe6ebcd3
1 module Exercise_3 where
2 import Data.List
3 import Data.Char (isSpace)
4
5 {- Library DO NOT CHANGE -}
6 type WrapFun = [Char] -> [Char]
7 type Picture = [[Char]]
8
9 printPicture :: Picture -> IO ()
10 printPicture [] = return ()
11 printPicture (xs : xss) = do
12 putStrLn xs
13 printPicture xss
14
15 pic = [".##.", ".#.#", ".###", "####"]
16 {- End Library -}
17
18
19 {---------------------------------------------------------------------}
20 {- Aufgabe H3.1 -}
21
22 snoc :: [a] -> a -> [a]
23 snoc [] y = [y]
24 snoc (x : xs) y = x : snoc xs y
25
26
27 member :: Eq a => a -> [a] -> Bool
28 member _ [] = False
29 member e (x : xs) = e == x || member e xs
30
31
32 butlast :: [a] -> [a]
33 butlast [] = []
34 butlast [_] = []
35 butlast (x : xs) = x : butlast xs
36
37
38 {---------------------------------------------------------------------}
39 {- Aufgabe G3.2 -}
40
41 uniq :: Eq a => [a] -> [a]
42 uniq (x:y:ys) = if x == y then uniq (y:ys) else x : uniq (y:ys)
43 uniq xs = xs
44
45
46 -- Alternativ:
47 uniq' :: Eq a => [a] -> [a]
48 uniq' [] = []
49 uniq' (x:xs) = f x xs
50 where f x [] = [x]
51 f x (y:ys) | x == y = f x ys
52 | otherwise = x : f y ys
53
54 uniqCount :: Eq a => [a] -> [(a, Integer)]
55 uniqCount [] = []
56 uniqCount (x:xs) = f (x,1) xs
57 where f p [] = [p]
58 f (x,c) (y:ys) | x == y = f (x, c + 1) ys
59 | otherwise = (x,c) : f (y, 1) ys
60
61
62
63 {---------------------------------------------------------------------}
64 {- Aufgabe G3.3 -}
65
66 intersep :: a -> [a] -> [a]
67 intersep sep (c : c' : cs) = c : sep : intersep sep (c' : cs)
68 intersep _ cs = cs
69
70
71 andList :: [[Char]] -> [Char]
72 andList [] = ""
73 andList [w] = w
74 andList [w1, w2] = w1 ++ " and " ++ w2
75 andList [w1, w2, w3] = w1 ++ ", " ++ w2 ++ ", and " ++ w3
76 andList (w : ws) = w ++ ", " ++ andList ws
77
78
79
80 {---------------------------------------------------------------------}
81 {- Aufgabe G3.4 -}
82
83 triangle :: [a] -> [(a, a)]
84 triangle [] = []
85 triangle (x : xs) = [(x, x') | x' <- xs] ++ triangle xs
86
87
88 {- QuickCheck properties -}
89 prop_triangle_base = triangle ([] :: [Int]) == []
90 prop_triangle_one x = triangle [x] == []
91 prop_triangle_two x y = triangle [x, y] == [(x, y)]
92 prop_triangle_length xs =
93 length (triangle xs) == n * (n - 1) `div` 2
94 where n = length xs
95 prop_triangle_distinct xs =
96 distinct xs ==> distinct (triangle xs)
97 where distinct ys = nub ys == ys
98 prop_triangle_complete x xs y ys = (x, y) `elem` triangle (x : xs ++ y : ys)
99 prop_triangle_sound1 x y xs =
100 not ((x, y) `elem` triangle (delete x (nub xs)))
101 && not ((y, x) `elem` triangle (delete x (nub xs)))
102 prop_triangle_rec x xs =
103 triangle (x : xs) == [(x, x') | x' <- xs] ++ triangle xs
104
105
106
107 {---------------------------------------------------------------------}
108 {- Aufgabe H3.1 -}
109
110 simplifySpaces :: [Char] -> [Char]
111 simplifySpaces = undefined
112
113
114
115 {---------------------------------------------------------------------}
116 {- Aufgabe H3.2 -}
117
118 prop_wrap1 :: WrapFun -> [Char] -> Bool
119 prop_wrap1 wrap xs = True
120
121 prop_wrap2 :: WrapFun -> [Char] -> Bool
122 prop_wrap2 wrap xs = True
123
124 prop_wrap3 :: WrapFun -> [Char] -> Bool
125 prop_wrap3 wrap xs = True
126
127 prop_wrap4 :: WrapFun -> [Char] -> Bool
128 prop_wrap4 wrap xs = True
129
130 prop_wrap5 :: WrapFun -> [Char] -> Bool
131 prop_wrap5 wrap xs = True
132
133 prop_wrap6 :: WrapFun -> [Char] -> Bool
134 prop_wrap6 wrap xs = True
135
136 prop_wrap7 :: WrapFun -> [Char] -> Bool
137 prop_wrap7 wrap xs = True
138
139 prop_wrap8 :: WrapFun -> [Char] -> Bool
140 prop_wrap8 wrap xs = True
141
142 prop_wrap9 :: WrapFun -> [Char] -> Bool
143 prop_wrap9 wrap xs = True
144
145 prop_wrap10 :: wrap xsFun -> [Char] -> Bool
146 prop_wrap10 wrap xs = True
147
148
149
150 {---------------------------------------------------------------------}
151 {- Aufgabe H3.3 -}
152
153 rotateClockwise :: Picture -> Picture
154 rotateClockwise = undefined
155
156
157
158 {---------------------------------------------------------------------}
159 {- Aufgabe H3.4 -}
160
161 {-WETT-}
162 sublist :: Eq a => [a] -> [a] -> Bool
163 sublist xs ys = undefined
164
165
166 subseq :: Eq a => [a] -> [a] -> Bool
167 subseq xs ys = undefined
168 {-TTEW-}