-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWeek03Problems.hs
236 lines (153 loc) · 6.13 KB
/
Week03Problems.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
module Week03Problems 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 True = False
invert 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
(b) what is 'mul 10'? How can you use it to multiply a number? -}
{- 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 = undefined -- fill this in
{- 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 = undefined
{- 5. Using 'map' with another function.
The function 'concat' concatenates a list 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 = undefined
{- 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 = undefined
numberOfEs :: String -> Int
numberOfEs = undefined
numberOf :: Char -> String -> Int
numberOf = undefined
{- 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'.
-}
filter_v2 :: (a -> Bool) -> [a] -> [a]
filter_v2 = undefined
filterMap :: (a -> Maybe b) -> [a] -> [b]
filterMap = undefined
{- 8. Composition
Write a function '>>>' that composes two functions. It 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' in the notes for
this week. -}
(>>>) :: (a -> b) -> (b -> c) -> a -> c
(>>>) = undefined
{- Try rewriting the 'numberOfEs' function from above using this one. -}
{- 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 = undefined
{- 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 = undefined
{- 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 = undefined
{- (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 = undefined
{- 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 = undefined
{- 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 = undefined