% !TEX program = xelatex
\documentclass{article}
\usepackage{../styles/style}
\usepackage{../styles/langs}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{marvosym}
\usepackage{boxedminipage}

\lstset{escapeinside={/*!}{!*/}}
\newcommand{\annotation}[1]{\hfill\footnotesize{}#1}

\usepackage{menukeys}


% Exact colors from NB
\usepackage[breakable]{tcolorbox}
\definecolor{incolor}{HTML}{303F9F}
\definecolor{outcolor}{HTML}{D84315}
\definecolor{cellborder}{HTML}{CFCFCF}
\definecolor{cellbackground}{HTML}{F7F7F7}


    
\begin{document}
\fnote{\copyright{} Christian Urban, King's College London, 2022}

\section*{Scala Worksheet 2}



\subsection*{Task 1 (Options)}

Get familiar with constructing strings and printing strings
(i.e.~\texttt{println}, \texttt{mkString}, \texttt{toString}, \ldots)

\begin{lstlisting}[numbers=none]
scala> List(7,2,3,4,5,6).find(_ < 4)
scala> List(5,6,7,8,9).find(_ < 4)
scala> 
\end{lstlisting}

\subsection*{Task 2 (URLs / Files)}

ALso Try

\begin{lstlisting}[numbers=none]
scala> 
scala> 
scala> 
\end{lstlisting}

\subsection*{Task 3 (Higher-Order Functions)}

Make sure you understand if-conditions in Scala: They are
\emph{expressions}, meaning they need to calculate a result. For
example an \texttt{if} without an else-branch is nearly always
\emph{not} what you intend to write (because you are not meant to
change any ``state'' outside the expression). Also, remember the quirks
in Scala with if-conditions needing parentheses and there is no
\texttt{then}-keyword.

\begin{lstlisting}[numbers=none]
scala> val s1 = "foo"
scala> val s2 = "bar"
scala  val s3 = "foobar"
scala> if (s1 == s2) print("equal") else print("unequal")
scala> if (s1 ++ s2 == s3) print("equal") else print("unequal")
\end{lstlisting}

\subsection*{Task 4 (Maps)}

Write \texttt{for}-comprehensions that enumerate all triples containing the numbers 1 - 5. That is,
print the list

\[
\texttt{(1,1,1)}, \texttt{(2,1,1)}, \texttt{(3,1,1)} \;\ldots\; \texttt{(5,5,5)}
\]

\noindent
Modify the \texttt{for}-comprehensions such that only triples are
printed where the sum is divisible by 3.  Then sort the list according
to the middle element (for this use \texttt{sortBy}).

\subsection*{Task 5 (Pattern-Matching)}

\subsection*{Task 6 (Web-Crawler)}

Write a pretty print function for lists of integers which
``condenses'' elements in the list, meaning if there is a number
several times in a row, then print out \mbox{\texttt{n x e}}, where
\texttt{n} is the number of repetitions and \texttt{e} is the
number. For example

\begin{lstlisting}[numbers=none]
  List(1,1,1,2,3,3)    =>   List(3 x 1, 2, 2 x 3)
  List(1,2,3,4,4,4)    =>   List(1, 2, 3, 3 x 4)
  List(1,1,1,1,1,1)    =>   List(6 x 1)
  List(1,1,1,2,1,1)    =>   List(3 x 1, 2, 2 x 1)
\end{lstlisting}

You might like to define a separate function that first counts the occurences
for each element and returns a list of (Int, Int)-pairs .

\end{document}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 
