| 
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  | 
  | 
| 
439
 | 
    28  | 
\section*{Scala Worksheet 1}
 | 
| 
188
 | 
    29  | 
  | 
| 
439
 | 
    30  | 
Please install Scala on your work-machine: You should have
  | 
| 
441
 | 
    31  | 
Scala up and running after this week, and also have an IDE that allows you to
  | 
| 
 | 
    32  | 
access the Scala REPL. Some installation instructions are given at
  | 
| 
170
 | 
    33  | 
  | 
| 
439
 | 
    34  | 
\begin{center}
 | 
| 
 | 
    35  | 
\url{https://www.scala-lang.org/download/2.13.10.html}
 | 
| 
 | 
    36  | 
\end{center}  
 | 
| 
395
 | 
    37  | 
  | 
| 
439
 | 
    38  | 
\subsubsection*{MacOSX}
 | 
| 
195
 | 
    39  | 
  | 
| 
439
 | 
    40  | 
\begin{itemize}
 | 
| 
 | 
    41  | 
\item[0)] (if needed) \texttt{brew install java} \;\;or\;\; \texttt{brew reinstall java}
 | 
| 
 | 
    42  | 
\item[1)] \texttt{brew install scala@2.13}
 | 
| 
 | 
    43  | 
\end{itemize}
 | 
| 
181
 | 
    44  | 
  | 
| 
439
 | 
    45  | 
\noindent Another method to install Scala on MacOSX
  | 
| 
181
 | 
    46  | 
  | 
| 
439
 | 
    47  | 
\begin{itemize} 
 | 
| 
 | 
    48  | 
\item[1)] \texttt{curl -s "https://get.sdkman.io" | bash}
 | 
| 
 | 
    49  | 
\item[2)] \texttt{sdk install scala 2.13.10}  
 | 
| 
 | 
    50  | 
\end{itemize}
 | 
| 
438
 | 
    51  | 
  | 
| 
439
 | 
    52  | 
\subsubsection*{Windows / Linux Ubuntu}
 | 
| 
438
 | 
    53  | 
  | 
| 
439
 | 
    54  | 
\begin{itemize}
 | 
| 
 | 
    55  | 
\item[0)] (if needed) \texttt{sudo apt-get remove scala-library scala}
 | 
| 
441
 | 
    56  | 
\item[1)] {\fontsize{8.5}{8.5}\selectfont\texttt{sudo wget https://downloads.lightbend.com/scala/2.13.10/scala-2.13.10.deb}}
 | 
| 
439
 | 
    57  | 
\item[2)] \texttt{sudo dpkg -i scala-2.13.10.deb}  
 | 
| 
 | 
    58  | 
\end{itemize}
 | 
| 
170
 | 
    59  | 
  | 
| 
 | 
    60  | 
\noindent
  | 
| 
439
 | 
    61  | 
Other Linux distros: \texttt{sudo apt-get scala}\bigskip
 | 
| 
182
 | 
    62  | 
  | 
| 
441
 | 
    63  | 
\noindent In the end you should have something running like in your terminal/shell:
  | 
| 
123
 | 
    64  | 
  | 
| 
 | 
    65  | 
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
 | 
| 
 | 
    66  | 
$ scala
  | 
| 
439
 | 
    67  | 
Welcome to Scala 2.13.10 (OpenJDK 64-Bit Server VM, Java 17.0.1).
  | 
| 
123
 | 
    68  | 
Type in expressions for evaluation. Or try :help.
  | 
| 
 | 
    69  | 
  | 
| 
 | 
    70  | 
scala>
  | 
| 
 | 
    71  | 
\end{lstlisting}%$
 | 
| 
 | 
    72  | 
  | 
| 
441
 | 
    73  | 
\noindent
  | 
| 
 | 
    74  | 
If you want to use VS Code or Codium, you might want to also look into setting up
  | 
| 
 | 
    75  | 
a key-shortcut for ``Run‐Selected‐Text‐In‐Active‐Terminal'' and also set up
  | 
| 
 | 
    76  | 
syntax highlighting. Have a look at the Scala handout uploaded to KEATS for information.\bigskip
  | 
| 
 | 
    77  | 
  | 
| 
 | 
    78  | 
\noindent
  | 
| 
 | 
    79  | 
Make sure you have checked out the template files for the Scala CWs and registered the
  | 
| 
 | 
    80  | 
Github repository on KEATS. See whether you receive a test report when pushing some code.
  | 
| 
439
 | 
    81  | 
\newpage
  | 
| 
181
 | 
    82  | 
  | 
| 
439
 | 
    83  | 
\subsection*{Task 1}
 | 
| 
123
 | 
    84  | 
  | 
| 
441
 | 
    85  | 
`Play' with the Scala REPL and try out the following queries. Observe
  | 
| 
 | 
    86  | 
what Scala responds. Does it make sense?
  | 
| 
123
 | 
    87  | 
  | 
| 
 | 
    88  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
    89  | 
scala> 2 + 2
  | 
| 
 | 
    90  | 
scala> 1 / 2
  | 
| 
 | 
    91  | 
scala> 1.0 / 2
  | 
| 
 | 
    92  | 
scala> 1 / 2.0
  | 
| 
 | 
    93  | 
scala> 1 / 0
  | 
| 
 | 
    94  | 
scala> 1.0 / 0.0
  | 
| 
 | 
    95  | 
scala> true == false
  | 
| 
 | 
    96  | 
scala> true && false
  | 
| 
 | 
    97  | 
scala> 1 > 1.0
  | 
| 
 | 
    98  | 
scala> "12345".length
  | 
| 
181
 | 
    99  | 
scala> List(1,2,1).size
  | 
| 
 | 
   100  | 
scala> Set(1,2,1).size
  | 
| 
265
 | 
   101  | 
scala> List(1) == List(1)
  | 
| 
439
 | 
   102  | 
scala> Set(1,2,3) == Set(3,2,1)
  | 
| 
265
 | 
   103  | 
scala> Array(1) == Array(1)
  | 
| 
 | 
   104  | 
scala> Array(1).sameElements(Array(1))
  | 
| 
335
 | 
   105  | 
\end{lstlisting}
 | 
| 
 | 
   106  | 
  | 
| 
439
 | 
   107  | 
\subsection*{Task 2 (Vals)}
 | 
| 
272
 | 
   108  | 
  | 
| 
441
 | 
   109  | 
Type in the value declarations below and observe Scala's responses. Explain what
  | 
| 
 | 
   110  | 
is printed as the value for \texttt{x} in the last line. Is \texttt{z} re-assigned
 | 
| 
 | 
   111  | 
in the 4th line?
  | 
| 
 | 
   112  | 
  | 
| 
 | 
   113  | 
\begin{lstlisting}[numbers=none]
 | 
| 
439
 | 
   114  | 
scala> val z = 42
  | 
| 
 | 
   115  | 
scala> z = z + 1
  | 
| 
441
 | 
   116  | 
scala> val x = 2 * z
  | 
| 
439
 | 
   117  | 
scala> val z = 466
  | 
| 
441
 | 
   118  | 
scala> println(x)
  | 
| 
 | 
   119  | 
\end{lstlisting}
 | 
| 
271
 | 
   120  | 
  | 
| 
441
 | 
   121  | 
  | 
| 
 | 
   122  | 
\subsection*{Task 3 (Strings)}
 | 
| 
 | 
   123  | 
  | 
| 
 | 
   124  | 
Get familiar with constructing strings and printing strings
  | 
| 
 | 
   125  | 
(i.e.~\texttt{println}, \texttt{mkString}, \texttt{toString}, \ldots)
 | 
| 
301
 | 
   126  | 
  | 
| 
 | 
   127  | 
\begin{lstlisting}[numbers=none]
 | 
| 
439
 | 
   128  | 
scala> println("Hello " ++ "World")
 | 
| 
441
 | 
   129  | 
scala> List(1,2,3,4).mkString("\n")
 | 
| 
 | 
   130  | 
scala> List(1,2,3,4).mkString("(", "|", ")")
 | 
| 
123
 | 
   131  | 
\end{lstlisting}
 | 
| 
 | 
   132  | 
  | 
| 
441
 | 
   133  | 
\subsection*{Task 4 (Functions)}
 | 
| 
 | 
   134  | 
  | 
| 
 | 
   135  | 
Write a function \texttt{miles2meters} taking an integer as argument
 | 
| 
 | 
   136  | 
and generating an integer. You can assume that one mile is 1609 meters.
  | 
| 
 | 
   137  | 
Remember that functions in Scala do not use the \texttt{return}-keyword.
 | 
| 
 | 
   138  | 
  | 
| 
 | 
   139  | 
\subsection*{Task 5 (If-Conditions)}
 | 
| 
 | 
   140  | 
  | 
| 
 | 
   141  | 
Make sure you understand if-conditions in Scala: They are
  | 
| 
 | 
   142  | 
\emph{expressions}, meaning they need to calculate a result. For
 | 
| 
 | 
   143  | 
example an \texttt{if} without an else-branch is nearly always
 | 
| 
 | 
   144  | 
\emph{not} what you intend to write (because you are not meant to
 | 
| 
 | 
   145  | 
change any ``state'' outside the expression). Also, remember the quirks
  | 
| 
 | 
   146  | 
in Scala with if-conditions needing parentheses and there is no
  | 
| 
 | 
   147  | 
\texttt{then}-keyword.
 | 
| 
 | 
   148  | 
  | 
| 
439
 | 
   149  | 
\begin{lstlisting}[numbers=none]
 | 
| 
441
 | 
   150  | 
scala> val s1 = "foo"
  | 
| 
 | 
   151  | 
scala> val s2 = "bar"
  | 
| 
 | 
   152  | 
scala  val s3 = "foobar"
  | 
| 
 | 
   153  | 
scala> if (s1 == s2) print("equal") else print("unequal")
 | 
| 
 | 
   154  | 
scala> if (s1 ++ s2 == s3) print("equal") else print("unequal")
 | 
| 
343
 | 
   155  | 
\end{lstlisting}
 | 
| 
 | 
   156  | 
  | 
| 
441
 | 
   157  | 
\subsection*{Task 6 (For-Comprehensions, Harder)}
 | 
| 
 | 
   158  | 
  | 
| 
 | 
   159  | 
Write \texttt{for}-comprehensions that enumerate all triples containing the numbers 1 - 5. That is,
 | 
| 
 | 
   160  | 
print the list
  | 
| 
 | 
   161  | 
  | 
| 
 | 
   162  | 
\[
  | 
| 
 | 
   163  | 
\texttt{(1,1,1)}, \texttt{(2,1,1)}, \texttt{(3,1,1)} \;\ldots\; \texttt{(5,5,5)}
 | 
| 
 | 
   164  | 
\]
  | 
| 
 | 
   165  | 
  | 
| 
 | 
   166  | 
\noindent
  | 
| 
 | 
   167  | 
Modify the \texttt{for}-comprehensions such that only triples are
 | 
| 
 | 
   168  | 
printed where the sum is divisible by 3.  Then sort the list according
  | 
| 
 | 
   169  | 
to the middle element (for this use \texttt{sortBy}).
 | 
| 
 | 
   170  | 
  | 
| 
 | 
   171  | 
\subsection*{Task 7 (Recursion, Advanced /Hard)}
 | 
| 
 | 
   172  | 
  | 
| 
 | 
   173  | 
Write a pretty print function for lists of integers which
  | 
| 
 | 
   174  | 
``condenses'' elements in the list, meaning if there is a number
  | 
| 
 | 
   175  | 
several times in a row, then print out \mbox{\texttt{n x e}}, where
 | 
| 
 | 
   176  | 
\texttt{n} is the number of repetitions and \texttt{e} is the
 | 
| 
 | 
   177  | 
number. For example
  | 
| 
 | 
   178  | 
  | 
| 
 | 
   179  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   180  | 
  List(1,1,1,2,3,3)    =>   List(3 x 1, 2, 2 x 3)
  | 
| 
 | 
   181  | 
  List(1,2,3,4,4,4)    =>   List(1, 2, 3, 3 x 4)
  | 
| 
 | 
   182  | 
  List(1,1,1,1,1,1)    =>   List(6 x 1)
  | 
| 
 | 
   183  | 
  List(1,1,1,2,1,1)    =>   List(3 x 1, 2, 2 x 1)
  | 
| 
 | 
   184  | 
\end{lstlisting}
 | 
| 
 | 
   185  | 
  | 
| 
 | 
   186  | 
You might like to define a separate function that first counts the occurences
  | 
| 
 | 
   187  | 
for each element and returns a list of (Int, Int)-pairs .
  | 
| 
 | 
   188  | 
  | 
| 
123
 | 
   189  | 
\end{document}
 | 
| 
 | 
   190  | 
  | 
| 
 | 
   191  | 
%%% Local Variables: 
  | 
| 
 | 
   192  | 
%%% mode: latex
  | 
| 
 | 
   193  | 
%%% TeX-master: t
  | 
| 
 | 
   194  | 
%%% End: 
  |