-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq3.tex
109 lines (93 loc) · 3.99 KB
/
q3.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
%%% Question 3 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\question This question is about user-defined types and type classes.
\begin{parts}
\part Consider the following data type:
\begin{verbatim}
data Layout a = Element a
| Vertical (Layout a) (Layout a)
| Horizontal (Layout a) (Layout a)
\end{verbatim}
The intuition here is that a layout consists of elements which are composed vertically or horizontally.
\begin{subparts}
\subpart[1] What is the type of the \texttt{\small Element} constructor? \droppoints
\begin{solution}
\emph{Comprehension.} \haskellIn{a -> Layout a}
\end{solution}
\subpart[2] What is the type of the \texttt{\small Vertical} constructor? \droppoints
\begin{solution}
\emph{Comprehension.} \haskellIn{Layout a -> Layout a -> Layout a}
\end{solution}
\subpart[1] What is the kind of the \texttt{\small Layout} type? \droppoints
\begin{solution}
\emph{Comprehension.} \haskellIn{* -> *}
\end{solution}
\subpart[4] The \texttt{\small Layout} type is a functor. Define a suitable instance of the \texttt{\small Functor} type class for it. \droppoints
\begin{solution}
\emph{Application.}
\begin{verbatim}
instance Functor Layout where
fmap f (Element a) = Element (f a)
fmap f (Vertical x y) = Vertical (fmap f x) (fmap f y)
fmap f (Horizontal x y) =
Horizontal (fmap f x) (fmap f y)
\end{verbatim}
\end{solution}
\end{subparts}
\part Consider the following definition:
\begin{verbatim}
example :: Layout (Sized Char)
example = Vertical
(Element (Sized (15,42) 'a'))
(Horizontal
(Element (Sized (4,16) 'b'))
(Element (Sized (23,8) 'c')))
\end{verbatim}
\begin{subparts}
\subpart[2] Define a suitable data type \texttt{\small Sized} so that the above definition is valid. \droppoints
\begin{solution}
\emph{Application.}
\begin{verbatim}
data Sized a = Sized (Int,Int) a
\end{verbatim}
\end{solution}
\subpart[4] Define a function
\begin{center}
\texttt{\small width~::~Layout (Sized a) -> Int}
\end{center}
which calculates the width of a layout. For example, \texttt{\small width example} should evaluate to \texttt{\small 27}. The width of horizontally composed layouts is the sum of the widths of the sub-layouts and the width of vertically composed layout is the maximum width of the sub-layouts. \droppoints
\begin{solution}
\emph{Application.}
\begin{verbatim}
width :: Layout (Sized a) -> Int
width (Element (Sized (x,y) a)) = x
width (Vertical a b) = max (width a) (width b)
width (Horizontal a b) = width a + width b
\end{verbatim}
\end{solution}
\subpart[4] Define a corresponding function
\begin{center}
\texttt{\small height~::~Layout (Sized a) -> Int}
\end{center}
which calculates the height of a layout. For example, \texttt{\small width example} should evaluate to \texttt{\small 58}. \droppoints
\begin{solution}
\emph{Application.}
\begin{verbatim}
height :: Layout (Sized a) -> Int
height (Element (Sized (x,y) a)) = y
height (Horizontal a b) = max (height a) (height b)
height (Vertical a b) = height a + height b
\end{verbatim}
\end{solution}
\subpart[7] Suppose that we wish to render a given layout to a surface that uses a coordinate system where the origin is the top left corner and that we need to know the top left coordinate of each element in a layout for this purpose. Define a function
\begin{center}
\texttt{\small pos~::~Layout (Sized a) -> Layout (Sized (Int,Int))}
\end{center}
which, for every element in the layout, calculates its absolute position and replaces its value with the calculated position. For example, \texttt{\small pos example} should evaluate to the following: \droppoints
\begin{verbatim}
Vertical (Element (Sized (15,42) (0,0)))
(Horizontal
(Element (Sized (4,16) (0,42)))
(Element (Sized (23,8) (4,42))))
\end{verbatim}
\end{subparts}
\end{parts}