| 197 |      1 | % !TEX program = xelatex
 | 
| 123 |      2 | \documentclass{article}
 | 
| 423 |      3 | \usepackage{../styles/style}
 | 
|  |      4 | \usepackage{../styles/langs}
 | 
| 272 |      5 | \usepackage{tikz}
 | 
|  |      6 | \usepackage{pgf}
 | 
| 123 |      7 | \usepackage{marvosym}
 | 
| 184 |      8 | \usepackage{boxedminipage}
 | 
| 123 |      9 | 
 | 
| 352 |     10 | \lstset{escapeinside={/*!}{!*/}}
 | 
|  |     11 | \newcommand{\annotation}[1]{\hfill\footnotesize{}#1}
 | 
| 272 |     12 | 
 | 
| 352 |     13 | \usepackage{menukeys}
 | 
| 335 |     14 | 
 | 
|  |     15 | 
 | 
|  |     16 | % Exact colors from NB
 | 
|  |     17 | \usepackage[breakable]{tcolorbox}
 | 
|  |     18 | \definecolor{incolor}{HTML}{303F9F}
 | 
|  |     19 | \definecolor{outcolor}{HTML}{D84315}
 | 
|  |     20 | \definecolor{cellborder}{HTML}{CFCFCF}
 | 
|  |     21 | \definecolor{cellbackground}{HTML}{F7F7F7}
 | 
| 334 |     22 | 
 | 
| 335 |     23 | 
 | 
|  |     24 |     
 | 
| 123 |     25 | \begin{document}
 | 
| 439 |     26 | \fnote{\copyright{} Christian Urban, King's College London, 2022}
 | 
| 195 |     27 | 
 | 
| 445 |     28 | \section*{Scala Worksheet 3}
 | 
| 271 |     29 | 
 | 
| 441 |     30 | 
 | 
| 442 |     31 | 
 | 
| 446 |     32 | \subsection*{Task 1 (Datatypes, Pattern-Matching)}
 | 
| 441 |     33 | 
 | 
| 446 |     34 | Define the datatype for binary trees where leaves contain an
 | 
|  |     35 | integer and inner nodes contain only a left and a right child.
 | 
|  |     36 | Use pattern-matching to define the following functions:
 | 
|  |     37 | 
 | 
| 301 |     38 | 
 | 
| 446 |     39 | \begin{itemize}
 | 
|  |     40 | \item[(1)] Define a function \texttt{mirror} that takes a tree as argument and returns
 | 
|  |     41 | the mirror-image of a tree (meaning left- and right-hand side are
 | 
|  |     42 | exchanged).
 | 
|  |     43 | \item[(2)] Define a function \texttt{sum\_tree} that sums up all
 | 
|  |     44 |   leaves in a tree.
 | 
|  |     45 | \end{itemize}  
 | 
| 123 |     46 | 
 | 
| 446 |     47 | \subsection*{Task 2 (Pattern-Matching, Accumulators)}
 | 
| 442 |     48 | 
 | 
| 446 |     49 | Define a function \texttt{remdups} that removes duplicates in a list,
 | 
|  |     50 | say list of integers. This functions should take a list of integers
 | 
|  |     51 | as argument and returns a list of integers. In order to remove
 | 
|  |     52 | elements that have already been seen, use an accumulator which can be
 | 
|  |     53 | a set of of integers (for fast lookup whether an element has already
 | 
|  |     54 | been seen). The accumulator can be implemented either with an
 | 
|  |     55 | auxiliary function or with a default argument like
 | 
| 441 |     56 | 
 | 
| 442 |     57 | \begin{lstlisting}[numbers=none]
 | 
| 446 |     58 | def remdups(xs: List[Int], acc: Set[Int] = Set()) : List[Int] = ???
 | 
| 343 |     59 | \end{lstlisting}
 | 
|  |     60 | 
 | 
| 444 |     61 | \noindent
 | 
| 446 |     62 | Are you familiar with polymorphic types in Scala? If yes, can you
 | 
|  |     63 | define \texttt{remdups} generically so that it works for any type
 | 
|  |     64 | of lists.
 | 
| 444 |     65 | 
 | 
| 446 |     66 | \subsection*{Task 3 (Pattern-Matching, Options, Maps)}
 | 
| 444 |     67 | 
 | 
| 446 |     68 | Remember that the method \texttt{indexOf} returns $-1$ whenever it
 | 
|  |     69 | cannot find an element in a list. Define a better version of
 | 
|  |     70 | this function, called \texttt{indexOfOption} that returns in
 | 
|  |     71 | the failure case \texttt{None} and in the success case
 | 
|  |     72 | \texttt{Some}$(n)$ where $n$ is the index of the element in the
 | 
|  |     73 | list. Try to define this function recursively but without an
 | 
|  |     74 | auxiliary function. For this use a \texttt{map}.
 | 
| 441 |     75 | 
 | 
|  |     76 | \begin{lstlisting}[numbers=none]
 | 
| 446 |     77 | scala> indexOfOption(List(1,2,3,4,5,6), 7)   // => None
 | 
|  |     78 | scala> indexOfOption(List(1,2,3,4,5,6), 4)   // => Some(3)
 | 
|  |     79 | scala> indexOfOption(List(1,2,3,4,3,6), 3)   // => Some(2)
 | 
| 441 |     80 | \end{lstlisting}
 | 
|  |     81 | 
 | 
| 446 |     82 | \subsection*{Task 4 (Pattern-Matching, If-guards,  hard)}
 | 
| 444 |     83 | 
 | 
| 446 |     84 | Recall the tree definition from Task 1. Define a \texttt{height}
 | 
|  |     85 | function that calculates the height of a tree.
 | 
| 444 |     86 | 
 | 
| 446 |     87 | Define a second function \texttt{balance} that makes sure that the
 | 
|  |     88 | height of a left and right child is balanced (only differ in heights
 | 
|  |     89 | by maximal of one). Implement this function by balancing trees
 | 
|  |     90 | stepwise - reorder that the height difference changes by one and then
 | 
|  |     91 | the balancing is repeated.
 | 
| 444 |     92 | 
 | 
| 446 |     93 | \subsection*{Task 5 (Fold, hard)}
 | 
| 444 |     94 | 
 | 
| 445 |     95 | Implement a function \texttt{fold} for lists of integers. It takes
 | 
|  |     96 | a list of integers as argument as well as a function $f$ and a unit element $u$.
 | 
| 446 |     97 | The function $f$ is of type \texttt{(Int, Int) => Int} and the unit element
 | 
|  |     98 | is of type \texttt{Int}. The return type of \texttt{fold} is \texttt{Int}.
 | 
| 445 |     99 | What is \texttt{fold} supposed to do? Well it should fold the function $f$
 | 
|  |    100 | over the elements of the list and in case of the empty list return the
 | 
| 446 |    101 | unit element $u$.
 | 
|  |    102 | %
 | 
|  |    103 | \[
 | 
|  |    104 |   \begin{array}{lcl}
 | 
|  |    105 |     \texttt{fold}(Nil, f, u) & \dn & u\\
 | 
|  |    106 |     \texttt{fold}(x::xs, f, u) & \dn & f(x, \texttt{fold}(xs, f, u))\\
 | 
|  |    107 |   \end{array}\smallskip  
 | 
|  |    108 | \]
 | 
|  |    109 | 
 | 
|  |    110 | \noindent
 | 
|  |    111 | Use \texttt{fold} to define the sum of a list of integers and the
 | 
|  |    112 | product of a list of integers. Try to use Scala's shorthand notation
 | 
|  |    113 | \texttt{\_ + \_} and \texttt{\_ * \_} for functions.
 | 
| 441 |    114 | 
 | 
| 123 |    115 | \end{document}
 | 
|  |    116 | 
 | 
|  |    117 | %%% Local Variables: 
 | 
|  |    118 | %%% mode: latex
 | 
|  |    119 | %%% TeX-master: t
 | 
|  |    120 | %%% End: 
 |