-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq1.tex
196 lines (158 loc) · 6.08 KB
/
q1.tex
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
%%% Question 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\question This question is about functional programming as a programming paradigm.
\makeatletter
\renewcommand{\verbatim@font}{\ttfamily \small}
\makeatother
\begin{parts}
\part For each of the following Haskell expressions, reduce it to its normal form.
\begin{subparts}
\subpart[1] \haskellIn{last "jedi"} \droppoints
\begin{solution}
\emph{Comprehension.} \haskellIn{'i'}
\end{solution}
\subpart[1] \haskellIn{reverse (filter (>'g') "uaogyfsetdichboa")} \droppoints
\begin{solution}
\emph{Comprehension.} \haskellIn{"ohitsyou"}
\end{solution}
\subpart[1] \haskellIn{fmap (fmap (\f a -> (*2) . f a)) (Just [(+),(-)])} \droppoints
\begin{solution}
\emph{Comprehension.}\\ \haskellIn{Just [\a -> (*2) . (+) a, \a -> (*2) . (-) a]}
\end{solution}
\subpart[1] {\small \texttt{foldr}\\
\texttt{\phantom{~~}(\textbackslash x r -> foldr take r x ++ r)}\\
\texttt{\phantom{~~}"NaBatman" [Just 4, Just 2]}} \droppoints
\begin{solution}
\emph{Comprehension.}\\ \haskellIn{"NaNaNaNaBatman"}
\end{solution}
\subpart[1]
\begin{verbatim}
let a = Just 4
b = Just 5
in (pure (+) <*> a <*> b) ==
\end{verbatim}
\texttt{\small \phantom{~~~}(a >{}>= \textbackslash x -> b >{}>= \textbackslash y -> pure (x+y))}
\droppoints
\begin{solution}
\emph{Comprehension.} \haskellIn{True}
\end{solution}
\end{subparts}
\part For each of the following expressions, choose the permissible type from the options that are listed if there is one. There is \emph{at most} one correct option for each expression. If no option is correct, please write ``not well typed''.
\begin{subparts}
\subpart[1] \haskellIn{drop 4} \droppoints
\begin{enumerate}
\item \haskellIn{Int -> [a] -> [a]}
\item \haskellIn{[a] -> [a]}
\item \haskellIn{[a]}
\item \haskellIn{Num a => [a]}
\item \haskellIn{Num a => (Int -> [a] -> [a]) -> a}
\end{enumerate}
\begin{solution}
\emph{Comprehension.} 2
\end{solution}
\subpart[1] \haskellIn{filter True [15==16,23==42]} \droppoints
\begin{enumerate}
\item \haskellIn{Bool -> [Bool] -> [Bool]}
\item \haskellIn{Bool -> [a] -> [a]}
\item \haskellIn{Bool -> [Int -> Int -> Bool] -> [Bool]}
\item \haskellIn{[Bool]}
\item \haskellIn{[Int -> Int -> Bool]}
\end{enumerate}
\begin{solution}
\emph{Comprehension.} Not well typed.
\end{solution}
%\ifprintanswers \pagebreak \else \fi
%\ifprintanswers \else \pagebreak \fi
\subpart[1] \haskellIn{let i=8+i in i} \droppoints
\begin{enumerate}
\item \haskellIn{a}
\item \haskellIn{Num a => a}
\item \haskellIn{a -> a}
\item \haskellIn{Num a => a -> a}
\item \haskellIn{Num a => (a,a)}
\end{enumerate}
\begin{solution}
\emph{Comprehension.} 2
\end{solution}
\subpart[1] \haskellIn{fmap . fmap} \droppoints
\begin{enumerate}
\item \haskellIn{Functor f => (f a -> f b) -> f a -> f b}
\item \haskellIn{(Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)}
\item \haskellIn{(Functor f, Functor g) => (f a -> g b) -> f a -> f b}
\item \haskellIn{Functor f => f a -> f b}
\item \haskellIn{(Functor f, Functor g) => f a -> g b}
\end{enumerate}
\begin{solution}
\emph{Comprehension.} 2
\end{solution}
\ifprintanswers \else \pagebreak \fi
\subpart[1] \haskellIn{\f -> (\x -> f (x x)) (\x -> f (x x))} \droppoints
\begin{enumerate}
\item \haskellIn{a -> b}
\item \haskellIn{(a -> b) -> b}
\item \haskellIn{(a -> b) -> (b -> b) -> b}
\item \haskellIn{(a -> (b -> b)) -> b}
\item \haskellIn{(a -> (b -> b)) -> (b -> b) -> b}
\end{enumerate}
\begin{solution}
\emph{Comprehension.} Not well typed.
\end{solution}
\end{subparts}
%\ifprintanswers \else \pagebreak \fi
\part[5] Consider the following definition of \haskellIn{map}:
\begin{small}
\begin{verbatim}
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xs
\end{verbatim}
\end{small}
Define a function \haskellIn{map'} which is equivalent to \haskellIn{map}, but is defined using \haskellIn{foldl} instead of explicit recursion. \droppoints
\begin{solution}
\emph{Application.} One possible answer is:
\begin{verbatim}
map' f = foldl (\r x -> r ++ [f x]) []
\end{verbatim}
\end{solution}
\part \label{part:sum}
\begin{subparts}
\subpart[5] \label{part:strict} Trace how \haskellIn{map' (+1) [4,8,15]} would be evaluated in a language with \emph{call-by-value} semantics. \droppoints
\begin{solution}
\emph{Comprehension.}
\begin{small}
\begin{verbatim}
map' (+1) [4,8,15]
=> foldl (\r x -> r ++ [(+1) x]) [] [4,8,15]
=> foldl (\r x -> r ++ [(+1) x]) ([] ++ [(+1) 4]) [8,15]
=> foldl (\r x -> r ++ [(+1) x]) ([] ++ [5]) [8,15]
=> foldl (\r x -> r ++ [(+1) x]) [5] [8,15]
=> foldl (\r x -> r ++ [(+1) x]) ([5] ++ [(+1) 8]) [15]
=> foldl (\r x -> r ++ [(+1) x]) ([5] ++ [9]) [15]
=> foldl (\r x -> r ++ [(+1) x]) [5,9] [15]
=> foldl (\r x -> r ++ [(+1) x]) ([5,9] ++ [(+1) 15]) []
=> foldl (\r x -> r ++ [(+1) x]) ([5,9] ++ [16]) []
=> foldl (\r x -> r ++ [(+1) x]) [5,9,16] []
=> [5,9,16]
\end{verbatim}
\end{small}
\end{solution}
\subpart[5] \label{part:lazy} Trace how \haskellIn{map' (+1) [4,8,15]} would be evaluated in a language with \emph{call-by-name} semantics. You should assume that the value of this expression is required by some other part of the program. \droppoints
\begin{solution} \emph{Comprehension.}
\begin{small}
\begin{verbatim}
map' (+1) [4,8,15]
=> foldl (\r x -> r ++ [(+1) x]) [] [4,8,15]
=> foldl (\r x -> r ++ [(+1) x]) ([] ++ [(+1) 4]) [8,15]
=> foldl (\r x -> r ++ [(+1) x]) (([] ++ [(+1) 4])
++ [(+1) 8]) [15]
=> foldl (\r x -> r ++ [(+1) x]) ((([] ++ [(+1) 4])
++ [(+1) 8]) ++ [(+1) 15]) []
=> (([] ++ [(+1) 4]) ++ [(+1) 8]) ++ [(+1) 15]
=> ([(+1) 4] ++ [(+1) 8]) ++ [(+1) 15]
=> [(+1) 4, (+1) 8] ++ [(+1) 15]
=> [(+1) 4, (+1) 8, (+1) 15]
=> [5,9,16]
\end{verbatim}
\end{small}
\end{solution}
\end{subparts}
\end{parts}