diff -r 80c691a4caab -r 7a0735db4788 wsheets/wsh01.tex --- a/wsheets/wsh01.tex Thu Nov 10 20:10:20 2022 +0000 +++ b/wsheets/wsh01.tex Mon Nov 14 12:04:21 2022 +0000 @@ -28,8 +28,8 @@ \section*{Scala Worksheet 1} Please install Scala on your work-machine: You should have -Scala up and running, and also an IDE that allows you to -access the Scala REPL. Some instructions are given at +Scala up and running after this week, and also have an IDE that allows you to +access the Scala REPL. Some installation instructions are given at \begin{center} \url{https://www.scala-lang.org/download/2.13.10.html} @@ -53,14 +53,14 @@ \begin{itemize} \item[0)] (if needed) \texttt{sudo apt-get remove scala-library scala} -\item[1)] {\fontsize{8.5}{8.5}\selectfont\texttt{sudo wget https://downloads.lightbend.com/scala/2.13.10/scala-2.13.7.deb}} +\item[1)] {\fontsize{8.5}{8.5}\selectfont\texttt{sudo wget https://downloads.lightbend.com/scala/2.13.10/scala-2.13.10.deb}} \item[2)] \texttt{sudo dpkg -i scala-2.13.10.deb} \end{itemize} \noindent Other Linux distros: \texttt{sudo apt-get scala}\bigskip -\noindent In the end you should have something running like +\noindent In the end you should have something running like in your terminal/shell: \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] $ scala @@ -70,12 +70,20 @@ scala> \end{lstlisting}%$ +\noindent +If you want to use VS Code or Codium, you might want to also look into setting up +a key-shortcut for ``Run‐Selected‐Text‐In‐Active‐Terminal'' and also set up +syntax highlighting. Have a look at the Scala handout uploaded to KEATS for information.\bigskip + +\noindent +Make sure you have checked out the template files for the Scala CWs and registered the +Github repository on KEATS. See whether you receive a test report when pushing some code. \newpage \subsection*{Task 1} -`Play' with the Scala REPL and try out the following querries. Observe -what Scala responds. +`Play' with the Scala REPL and try out the following queries. Observe +what Scala responds. Does it make sense? \begin{lstlisting}[numbers=none] scala> 2 + 2 @@ -98,22 +106,86 @@ \subsection*{Task 2 (Vals)} +Type in the value declarations below and observe Scala's responses. Explain what +is printed as the value for \texttt{x} in the last line. Is \texttt{z} re-assigned +in the 4th line? + +\begin{lstlisting}[numbers=none] scala> val z = 42 scala> z = z + 1 +scala> val x = 2 * z scala> val z = 466 +scala> println(x) +\end{lstlisting} -\subsection*{Task 3} + +\subsection*{Task 3 (Strings)} + +Get familiar with constructing strings and printing strings +(i.e.~\texttt{println}, \texttt{mkString}, \texttt{toString}, \ldots) \begin{lstlisting}[numbers=none] scala> println("Hello " ++ "World") -scala> +scala> List(1,2,3,4).mkString("\n") +scala> List(1,2,3,4).mkString("(", "|", ")") \end{lstlisting} +\subsection*{Task 4 (Functions)} + +Write a function \texttt{miles2meters} taking an integer as argument +and generating an integer. You can assume that one mile is 1609 meters. +Remember that functions in Scala do not use the \texttt{return}-keyword. + +\subsection*{Task 5 (If-Conditions)} + +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> println("Hello " ++ "World") -scala> +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 6 (For-Comprehensions, Harder)} + +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 7 (Recursion, Advanced /Hard)} + +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: