-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWeek03Solutions.hs
372 lines (251 loc) · 9.85 KB
/
Week03Solutions.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
module Week03Solutions where
import Data.Char
{------------------------------------------------------------------------------}
{- TUTORIAL QUESTIONS -}
{------------------------------------------------------------------------------}
{- 1. Lambda notation.
Rewrite the following functions using the '\x -> e' notation (the
"lambda" notation), so that they are written as 'double =
<something>', and so on. -}
mulBy2 :: Int -> Int
mulBy2 = \x -> 2*x
mul :: Int -> Int -> Int
mul = \x y -> x * y
invert :: Bool -> Bool
invert = -- \x -> if x then False else True
-- not
\x -> case x of
True -> False
False -> True
{- HINT: use a 'case', or an 'if'. -}
{- 2. Partial Application
The function 'mul' defined above has the type 'Int -> Int ->
Int'. (a) What is the type of the Haskell expression:
mul 10
ANSWER: mul 10 has type 'Int -> Int'
(b) what is 'mul 10'? How can you use it to multiply a number?
ANSWER: 'mul 10' is a function that multiplies its argument bt
10. You can use to do this multiplication by applying it to a
value. So 'mul 10 20' gives the answer 200.
-}
{- 3. Partial Application
Write the 'mulBy2' function above using 'mul'. Can you make your
function as short as possible? -}
double_v2 :: Int -> Int
double_v2 = mul 2
{- The longer version is:
double_v2 x = mul 2 x
but every time you have a function definition that looks like this:
fname x y z = <something> z
it can be shortened to:
fname x y = <something>
-}
{- 4. Using 'map'.
The function 'toUpper' takes a 'Char' and turns lower case
characters into upper cases one. All other characters it returns
unmodified. For example:
> toUpper 'a'
'A'
> toUpper 'A'
'A'
Strings are lists of characters. 'map' is a function that applies a
function to every character in a list and returns a new list.
Write the function 'shout' that uppercases a string, so that:
> shout "hello"
"HELLO"
-}
shout :: String -> String -- remember that String = [Char]
shout = map toUpper
{- Longer version:
shout xs = map toUpper xs
-}
{- 5. Using 'map' with another function.
The function 'concat' concatenates a lists of lists to make one
list:
> concat [[1,2],[3,4],[5,6]]
[1,2,3,4,5,6]
Using 'map', 'concat', and either a helper function or a function
written using '\', write a function 'dupAll' that duplicates every
element in a list. For example:
> dupAll [1,2,3]
[1,1,2,2,3,3]
> dupAll "my precious"
"mmyy pprreecciioouuss"
HINT: try writing a helper function that turns single data values
into two element lists. -}
dupAll :: [a] -> [a]
dupAll xs = concat (map (\x -> [x,x]) xs)
{- A shorter version is this:
dupAll = concat . map (\x -> [x,x])
which uses the composition operator '.'
-}
-- [1,2,3]
-- [[1,1],[2,2],[3,3]]
-- [1,1,2,2,3,3]
{- Compare this to the recursive version:
dupAll [] = []
dupAll (x:xs) = x : x : dupAll xs
The difference between this and the definition using 'map' is that
it mixes the concerns of 'duplicate an element' and 'do this to
every element'. The version using 'map' explicitly makes clear that
something is happening to every element of the list.
In a small example like this, it is not immediately clear which
version is easier, but when the amount of things we want to do to
every element gets larger, map can often be clearer to show intent. -}
{- 6. Using 'filter'
(a) Use 'filter' to return a list consisting of only the 'E's in
a 'String'.
(b) Use 'onlyEs' and 'length' to count the number of 'E's in a string.
(c) Write a single function that takes a character 'c' and a string
's' and counts the number of 'c's in 's'. -}
onlyEs :: String -> String
onlyEs = filter (\x -> x == 'E')
numberOfEs :: String -> Int
numberOfEs xs = length (onlyEs xs)
{- A shorter version is:
numberOfEs = length . onlyEs
-}
numberOf :: Char -> String -> Int
numberOf c = length . filter (\x -> x == c)
{- 7. Rewriting 'filter'
(a) Write a function that does the same thing as filter, using
'map' and 'concat'.
(b) Write a function that does a 'map' and a 'filter' at the same
time, again using 'map' and 'concat'.
-}
{- This is idea of the solution below. If the predicate is "\x -> x == 'E'", then we map every 'E' to ['E'] and everything else to []. Then we concatenate all the lists.
['E', 'I','E', 'I','O']
==> [['E'],[], ['E'], [], [] ]
==> ['E', 'E' ]
-}
filter_v2 :: (a -> Bool) -> [a] -> [a]
filter_v2 p = concat . map (\x -> if p x then [x] else [])
filterMap :: (a -> Maybe b) -> [a] -> [b]
filterMap p = concat . map (\x -> case p x of
Nothing -> []
Just y -> [y])
{- 8. Composition
Write a function '>>>' that composes two functions: takes two
functions 'f' and 'g', and returns a function that first runs 'f'
on its argument, and then runs 'g' on the result.
HINT: this is similar to the function 'compose'. -}
(>>>) :: (a -> b) -> (b -> c) -> a -> c
(>>>) f g x = g (f x)
-- NOTE: the functions 'f' and 'g' appear the other way round in
-- the function body.
{- Try rewriting the 'numberOfEs' function from above using this one. -}
numberOfEs_v2 = filter (\x -> x == 'E') >>> length
{- 9. Backwards application
Write a function of the following type that takes a value 'x' and a
function 'f' and applies 'f' to 'x'. Note that this functions takes
its arguments in reverse order to normal function application! -}
(|>) :: a -> (a -> b) -> b
(|>) x f = f x
{- This function can be used between its arguments like so:
"HELLO" |> map toLower
and it is useful for chaining calls left-to-right instead of
right-to-left as is usual in Haskell:
"EIEIO" |> onlyEs |> length
-}
{- 10. Flipping
Write a function that takes a two argument function as an input,
and returns a function that does the same thing, but takes its
arguments in reverse order: -}
flip :: (a -> b -> c) -> b -> a -> c
flip f a b = f b a
{- 11. Evaluating Formulas
Here is a datatype describing formulas in propositional logic, as
in CS208 last year. Atomic formulas are represented as 'String's. -}
data Formula
= Atom String
| And Formula Formula
| Or Formula Formula
| Not Formula
deriving Show
{- (a) Write a function that evaluates a 'Formula' to a 'Bool'ean value,
assuming that all the atomic formulas are given the value
'True'. Note that the following Haskell functions do the basic
operations on 'Bool'eans:
(&&) :: Bool -> Bool -> Bool -- 'AND'
(||) :: Bool -> Bool -> Bool -- 'OR'
not :: Bool -> Bool -- 'NOT'
-}
eval_v1 :: Formula -> Bool
eval_v1 (Atom a) = True
eval_v1 (And p q) = eval_v1 p && eval_v1 q
eval_v1 (Or p q) = eval_v1 p || eval_v1 q
eval_v1 (Not p) = not (eval_v1 p)
{- (b) Now write a new version of 'eval_v1' that, instead of evaluating
every 'Atom a' to 'True', takes a function that gives a 'Bool'
for each atomic proposition: -}
eval :: (String -> Bool) -> Formula -> Bool
eval v (Atom a) = v a
eval v (And p q) = eval v p && eval v q
eval v (Or p q) = eval v p || eval v q
eval v (Not p) = not (eval v p)
{- For example:
eval (\s -> s == "A") (Or (Atom "A") (Atom "B")) == True
eval (\s -> s == "A") (And (Atom "A") (Atom "B")) == False
-}
{- 12. Substituting Formulas
Write a function that, given a function 's' that turns 'String's
into 'Formula's (a "substitution"), replaces all the atomic
formulas in a Formula with whatever 'f' tells it to: -}
subst :: (String -> Formula) -> Formula -> Formula
subst v (Atom a) = v a
subst v (And p q) = subst v p `And` subst v q
subst v (Or p q) = subst v p `Or` subst v q
subst v (Not p) = Not (subst v p)
{- For example:
subst (\s -> if s == "A" then Not (Atom "A") else Atom s) (And (Atom "A") (Atom "B")) == And (Not (Atom "A")) (Atom "B")
-}
{- 13. Evaluating with failure
The 'eval' function in 8(b) assumed that every atom could be
assigned a value. But what if it can't? Write a function of the
following type that takes as input a function that may or may not
give a 'Bool' for each atom, and correspondingly, may or may not
give a 'Bool' for the whole formula. -}
evalMaybe :: (String -> Maybe Bool) -> Formula -> Maybe Bool
evalMaybe v (Atom a) = v a
evalMaybe v (And p q) =
case evalMaybe v p of
Nothing -> Nothing
Just x ->
case evalMaybe v q of
Nothing -> Nothing
Just y ->
Just (x && y)
evalMaybe v (Or p q) =
case evalMaybe v p of
Nothing -> Nothing
Just x ->
case evalMaybe v q of
Nothing -> Nothing
Just y ->
Just (x || y)
evalMaybe v (Not p) =
case evalMaybe v p of
Nothing -> Nothing
Just x ->
Just (not x)
{- This is pretty complex and noisy looking, because it makes all the
error handling explicit. On the other hand, it is easy to trace
what will happen in all of the possible cases, including those that
happen when there are errors. We will see ways of making it look
nicer in Weeks 6 & 7.
One thing to think about is why the 'Atom a' case is this:
evalMaybe v (Atom a) = v a
and not:
evalMaybe v (Atom a) =
case v a of
Nothing -> Nothing
Just x -> Just x
the answer is that anything like:
case <something> of
Nothing -> Nothing
Just x -> Just x
is always equal to '<something>' -- the 'case' is returning
'Nothing' when the value is 'Nothing' and 'Just x' when it is 'Just
x', so in the end it does nothing to the value returned by
'<something>' and can be removed. -}