forked from mbg/uow-exam
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathq3.tex
More file actions
188 lines (159 loc) · 7.14 KB
/
Copy pathq3.tex
File metadata and controls
188 lines (159 loc) · 7.14 KB
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
%%% Question 3 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\question This question is about user-defined types and type classes.
\begin{parts}
\part Consider a variant of rose trees where elements are stored in nodes rather than leaves such that, for example, the following is a valid definition:
\begin{small}
\begin{minted}{haskell}
tree :: Tree String
tree = Node "Sakura" [Node "Rubber" []]
\end{minted}
\end{small}
\begin{subparts}
\subpart[3] Give a suitable definition for the \haskellIn{Tree} type. \droppoints
\begin{solution}
\emph{Application.}
\begin{small}
\begin{minted}{haskell}
data Tree a = Node a [Tree a]
\end{minted}
\end{small}
\end{solution}
\subpart[1] A collection of trees is referred to as a \emph{forest}. Define a suitable \emph{type alias} for \haskellIn{Forest a} which represents zero or more trees of elements of type \haskellIn{a}. \droppoints
\begin{solution}
\emph{Application.}
\begin{small}
\begin{minted}{haskell}
type Forest a = [Tree a]
\end{minted}
\end{small}
\end{solution}
\subpart[4] The \haskellIn{Tree} type is a functor. Define a suitable instance of Haskell's \haskellIn{Functor} type class for it. Your solution should obey the functor laws, although you do not need to prove this. \droppoints
\begin{solution}
\emph{Application.}
\begin{small}
\begin{minted}{haskell}
instance Functor Tree where
fmap f (Node x ts) = Node (f x) (fmap (fmap f) ts)
\end{minted}
\end{small}
\end{solution}
\subpart[4] Define a suitable instance of Haskell's \haskellIn{Foldable} type class for the \haskellIn{Tree} type. \droppoints
\begin{solution}
\emph{Application.}
\begin{small}
\begin{minted}{haskell}
instance Foldable Tree where
foldr f z (Node x ts) =
f x (foldr (\t r -> foldr f r t) z ts)
\end{minted}
\end{small}
\end{solution}
\end{subparts}
\part A zipper is a purely functional data structure which can be used to efficiently traverse another data structure if the same element needs to be accessed repeatedly or element access is relative to the last-accessed element by allowing a particular element of the data structure to be ``focused''. Suppose that we have an implementation of binary trees as follows:
\begin{small}
\begin{minted}{haskell}
data BinTree a = BinLeaf a | BinNode (BinTree a) (BinTree a)
\end{minted}
\end{small}
\begin{subparts}
\subpart[2] Define an algebraic data type named \haskellIn{Direction} so that it has two constructors with the following types:\\
\haskellIn{L :: BinTree a -> Direction a} \\
\haskellIn{R :: BinTree a -> Direction a} \droppoints
\begin{solution}
\emph{Application.}
\begin{small}
\begin{minted}{haskell}
data Direction a = L (BinTree a) | R (BinTree a)
\end{minted}
\end{small}
\end{solution}
\subpart[1] Consider the following example definition that represents a value of a zipper on a \haskellIn{BinTree} in which we have already moved the ``focus'' from the root to the left child:
\vspace*{0.2cm}
\begin{minted}{haskell}
example :: BinZipper String
example = Pos [L (BinLeaf "Right child")]
(BinLeaf "Left child")
\end{minted}
\vspace*{0.2cm}
Give a suitable definition of the \haskellIn{BinZipper} type. \droppoints
\begin{solution}
\emph{Comprehension.}
\begin{minted}{haskell}
data BinZipper a = Pos [Direction a] (BinTree a)
\end{minted}
\end{solution}
\subpart[1] A value of the \haskellIn{BinZipper} type can be used to represent a position within a binary tree, where \haskellIn{[Direction a]} represents a list of directions that were taken from the root and \haskellIn{BinTree a} is the node or leaf that is currently in ``focus''. Define a function
\vspace*{0.2cm}
\begin{minted}{haskell}
fromTree :: BinTree a -> BinZipper a
\end{minted}
\vspace*{0.2cm}
which can be used to create an initial zipper for a given binary tree. \droppoints
\begin{solution}
\emph{Application.}
\begin{small}
\begin{minted}{haskell}
fromTree :: BinTree a -> BinZipper a
fromTree t = Pos [] t
\end{minted}
\end{small}
\end{solution}
\subpart[2] Define a function
\vspace*{0.2cm}
\begin{minted}{haskell}
view :: BinZipper a -> Maybe a
\end{minted}
\vspace*{0.2cm}
which gets the element that is currently in the view, if there is one. For example, \haskellIn{view (Pos p (BinLeaf x))} should evaluate to \haskellIn{Just x}. \droppoints
\begin{solution}
\emph{Application.}
\begin{small}
\begin{minted}{haskell}
view :: BinZipper a -> Maybe a
view (Pos ds (BinLeaf x)) = Just x
view (Pos ds _) = Nothing
\end{minted}
\end{small}
\end{solution}
\ifprintanswers \else \pagebreak \fi
\subpart[4] Define two functions
\vspace*{0.2cm}
\begin{minted}{haskell}
turnRight :: BinZipper a -> Maybe (BinZipper a)
turnLeft :: BinZipper a -> Maybe (BinZipper a)
\end{minted}
\vspace*{0.2cm}
which, given a current position in a tree, go down the right or left subtree, respectively. For example, \haskellIn{turnRight (Pos [] (BinNode l r))} should evaluate to \haskellIn{Just (Pos [R l] r)}. \droppoints
\begin{solution}
\emph{Application.}
\begin{small}
\begin{minted}{haskell}
turnRight :: BinZipper a -> Maybe (BinZipper a)
turnRight (Pos ds (BinNode l r)) = Just (Pos (R l:ds) r)
turnRight (Pos ds (BinLeaf _)) = Nothing
turnLeft :: BinZipper a -> Maybe (BinZipper a)
turnLeft (Pos ds (BinNode l r)) = Just (Pos (L r:ds) l)
turnLeft (Pos ds (BinLeaf _)) = Nothing
\end{minted}
\end{small}
\end{solution}
\subpart[3] Define a function
\vspace*{0.2cm}
\begin{minted}{haskell}
back :: BinZipper a -> Maybe (BinZipper a)
\end{minted}
\vspace*{0.2cm}
which shifts the focus of the zipper up the tree to the parent of the node that is initially in focus. \droppoints
\begin{solution}
\emph{Application.}
\begin{small}
\begin{minted}{haskell}
back :: BinZipper a -> Maybe (BinZipper a)
back (Pos [] _) = Nothing
back (Pos (L r:ds) t) = Just (Pos ds (BinNode t r))
back (Pos (R l:ds) t) = Just (Pos ds (BinNode l t))
\end{minted}
\end{small}
\end{solution}
\end{subparts}
\end{parts}