80 lines
4.5 KiB
TeX

\input{../settings/settings}
\begin{document}
\klausur{Nichtprozedurale Programmierung}{Prof. M. Mendler}{Wintersemester 11/12}{90}{Wörterbuch}
\begin{enumerate}
\item{
\begin{enumerate}
\item{Recall that in the $\lambda$-calculus the Church Boolean constant can be coded as tt $=_{df} \lambda t.\lambda f t$ for True and $=_{df} \lambda t.\lambda f f$ for False. recall also that logical negation can be represented with the combinator not $= \lambda x. x ff tt$. For this question consider the combinator:\\
$C =_{df} \lambda p.\lambda q.p(not q)(not p)$\\
Verify by the means of $\beta$-reductions that C tt ff produces the Church Boolean tt. Indicate the type of evaluation strategy (eager, lazy or none of them) you are employing for doing this and present every reduction step in detail.}
\item{State the $\lambda$-terms for the lazy and eager recursion fixedpoint combinators $Y^*$ and Y.}
\end{enumerate}
}
\item{Consider the following Haskell function: \\
\texttt{mystery::(a$\rightarrow$b$\rightarrow$c)$\rightarrow$a$\rightarrow$[b]$\rightarrow$a\\
mystery f e [] = e\\
mystery f e (x:xs) = mystery f (f e x) xs\\ }
Now take the following Haskell function \textit{commute} which receives a binary function \textit{op} and two input arguments. The function \textit{commute} applies \textit{op} to its input arguments while changing the order of the operants:\\
\texttt{commute::(a$\rightarrow$b$\rightarrow$c)$\rightarrow$b$\rightarrow$a$\rightarrow$c\\
commute op = $\backslash$x $\rightarrow \backslash$y$\rightarrow$ y 'op' x\\}
Compute the result of the following expression:\\
\texttt{mystery(commute(:))[][1,2..10]\\}
Explain your workings!
}
\item{
A binary search tree is an element of the datatype\\
\texttt{data BStree a = Nil | Fork (BStree a) a (BStree a) deriving(Show, Eq, Ord)\\}
The constructor Nil denotes the empty tree, Fork introduces a tree that consists of a node with a left subtree, a label and a right subtree.\\
\begin{enumerate}
\item{
State the induction principle for the datatype BStree of finite binary search trees.}
\item{A binary search tree is called height-balanced if for each node the height of the left subtree differs from the height of the right subtree by at most 1. Define a Haskell function \texttt{balancedT::BStree a $\rightarrow$ Bool} such that \texttt{balancedT st} returns True if the binary search tree st is height-balanced, False otherwise. As an illustration consider the binary search trees st1, st2 defined as follows:\\
\texttt{st1 = Fork (Fork Nil 2 (Fork Nil 3 Nil)) 5 (Fork (Fork Nil 7 Nil) 9 Nil)\\
st2 = Fork (Fork Nil 2 (Fork Nil 3 Nil) 4 Nil)) 5 (Fork (fork Nil 7 Nil) 9 Nil)\\}
Then:\\
\texttt{Main\textgreater balancedT Nil\\
True\\
Main\textgreater balancedT st1\\
True\\
Main\textgreater balancedT st2\\
False\\}
To construct banlancedT you may find it useful to define a function \texttt{heightT::BStree a $\rightarrow$ Int} first which returns the maximum lenght of any path from the root node of a BStree to one of its leaves. The lenght of a path is the number of value nodes that it contains. For instance:
\texttt{Main\textgreater heightT Nil\\
True\\
Main\textgreater heightT (Fork Nil 5 Nil)\\
1\\
Main\textgreater heightT st1\\
3\\
Main\textgreater heightT st2\\
4\\}
}
\end{enumerate}
\item{
Determine whether or not there exists a monotype $\tau$ for the following closed F2 expression:\\
\texttt{$e =_{df} fn x \Rightarrow fn y \Rightarrow fn a \Rightarrow \textless (\# 1x) a,(\# 2x) y\textgreater end in z y end end end$\\}
If a monotype exists give an instance of $\tau$ and show that [] $\vdash$ e : $\tau$ using the F2 typing rules given in the Appendix. Otherwise, if your answer is no, justify why there cannot be a monotype for e.\\
}
\item{
Define a Haskell function\\
\texttt{my\_clock:: Str Bool}\\
that produces the stream\\
\texttt{[True, False, False, False, True, False, False, False..]}\\
by using the dataflow constructors (fby, s\_not, s\_true,...) from the lectures and s\_clock\_1 from the tutorials. Recall that s\_clock\_1 takes an Integern and generates a stream of Boolean consisitng of repeating blocks of n successive values True separated by a single occurence of False. The function s\_clock\_1 is defined as follows:
\texttt{
\begin{tabbing}
s\_clock\_1:: Int $\rightarrow$ Str Bool\\
s\_clock\_1 n \= \kill
s\_clock\_1 n \>| n == 1 = s\_half\\
\>| odd n = merge s\_half s\_true (s\_clock\_1 (div (n-1) 2))\\
\>| otherwise = \= when( merge s\_half (s\_clock\_1 (div n 2)) s\_true)\\
\>\> (s\_clock\_1 (n+1))\\
\end{tabbing}
}
}
}
\end{enumerate}
Appendix:\\
Monomorphic Typing Rules for F2\\
\end{document}