# HG changeset patch # User Christian Urban # Date 1668427461 0 # Node ID 7a0735db47881971dafdb288e3e3f13095c9bceb # Parent 80c691a4caabe859e889995f18719aebeb15ad23 updated diff -r 80c691a4caab -r 7a0735db4788 cws/core_cw01.pdf Binary file cws/core_cw01.pdf has changed diff -r 80c691a4caab -r 7a0735db4788 cws/core_cw02.pdf Binary file cws/core_cw02.pdf has changed diff -r 80c691a4caab -r 7a0735db4788 cws/core_cw03.pdf Binary file cws/core_cw03.pdf has changed diff -r 80c691a4caab -r 7a0735db4788 cws/main_cw01.pdf Binary file cws/main_cw01.pdf has changed diff -r 80c691a4caab -r 7a0735db4788 cws/main_cw02.pdf Binary file cws/main_cw02.pdf has changed diff -r 80c691a4caab -r 7a0735db4788 cws/main_cw03.pdf Binary file cws/main_cw03.pdf has changed diff -r 80c691a4caab -r 7a0735db4788 cws/main_cw03.tex --- a/cws/main_cw03.tex Thu Nov 10 20:10:20 2022 +0000 +++ b/cws/main_cw03.tex Mon Nov 14 12:04:21 2022 +0000 @@ -237,7 +237,7 @@ \end{center} \noindent -The alternative regular expressions comes in two versions: one is +The alternative regular expression comes in two versions: one is binary (+ / \texttt{ALT}) and the other is n-ary ($\sum$ / \texttt{ALTs}). The latter takes a list of regular expressions as argument. In what follows we shall use $rs$ to stand for lists of diff -r 80c691a4caab -r 7a0735db4788 cws/main_cw04.pdf Binary file cws/main_cw04.pdf has changed diff -r 80c691a4caab -r 7a0735db4788 cws/main_cw04.tex --- a/cws/main_cw04.tex Thu Nov 10 20:10:20 2022 +0000 +++ b/cws/main_cw04.tex Mon Nov 14 12:04:21 2022 +0000 @@ -173,7 +173,7 @@ computer. For example you can call Scala on the command line with the option \texttt{-cp knight1.jar} and then query any function from the \texttt{knight1.scala} template file. As usual you have to -prefix the calls with \texttt{M4a}, \texttt{M4b} and \texttt{M4c}. +prefix the calls with \texttt{M4a}, \texttt{M4b}, \texttt{M4c} and \texttt{M4d}. Since some of the calls are time sensitive, I included some timing information. For example diff -r 80c691a4caab -r 7a0735db4788 cws/main_cw05.pdf Binary file cws/main_cw05.pdf has changed diff -r 80c691a4caab -r 7a0735db4788 progs/lecture1.scala --- a/progs/lecture1.scala Thu Nov 10 20:10:20 2022 +0000 +++ b/progs/lecture1.scala Mon Nov 14 12:04:21 2022 +0000 @@ -436,6 +436,7 @@ // - no mutable data-structures (no Arrays, no ListBuffers) // But what the heck....lets try to count to 1 Mio in parallel +import scala.collection.parallel.CollectionConverters._ var cnt = 0 @@ -452,7 +453,7 @@ def count_intersection(A: Set[Int], B: Set[Int]) : Int = { var count = 0 - for (x <- A.par; if (B contains x)) count += 1 + for (x <- A; if (B contains x)) count += 1 count } diff -r 80c691a4caab -r 7a0735db4788 progs/mandelbrot.scala --- a/progs/mandelbrot.scala Thu Nov 10 20:10:20 2022 +0000 +++ b/progs/mandelbrot.scala Mon Nov 14 12:04:21 2022 +0000 @@ -3,7 +3,7 @@ // // see https://en.wikipedia.org/wiki/Mandelbrot_set // -// under scala 2.13.1 needs to be called with +// under scala 2.13.XX needs to be called with // // scala -cp scala-parallel-collections_2.13-0.2.0.jar mandelbrot.scala diff -r 80c691a4caab -r 7a0735db4788 wsheets/wsh01.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wsheets/wsh01.scala Mon Nov 14 12:04:21 2022 +0000 @@ -0,0 +1,109 @@ + +// Task 1 + +2 + 2 +1 / 2 +1.0 / 2 +1 / 2.0 +1 / 0 +1.0 / 0.0 +true == false +true && false +1 > 1.0 +"12345".length +List(1,2,1).size +Set(1,2,1).size +List(1) == List(1) +Set(1,2,3) == Set(3,2,1) +Array(1) == Array(1) +Array(1).sameElements(Array(1)) + + +// Task 2 + +val z = 42 +z = z + 1 . // error +val x = 2 * z +val z = 466 // old z not accessible anymore +println(x) + + +// Task 3 +println("Hello " ++ "World") +List(1,2,3,4).mkString("\n") +List(1,2,3,4).mkString("(", "|", ")") + + +// Task 4 +def miles2meters(m: Int): Int = m * 1609 + + +// Task 5 + +val s1 = "foo" +val s2 = "bar" +val s3 = "foobar" +if (s1 == s2) print("equal") else print("unequal") +if (s1 ++ s2 == s3) print("equal") else print("unequal") + + +// Task 6 +for (x <- (1 to 5).toList; + y <- (1 to 5).toList; + z <- (1 to 5).toList) yield (x,y,z) + +for (x <- (1 to 5).toList; + y <- (1 to 5).toList; + z <- (1 to 5).toList; + if (x + y + z) % 3 == 0) yield (x,y,z) + +(for (x <- (1 to 5).toList; + y <- (1 to 5).toList; + z <- (1 to 5).toList; + if (x + y + z) % 3 == 0) yield (x,y,z)).sortBy(_._2) + + + +// Task 7 + +// first version with using an accumulator + +def cnt(xs: List[Int], acc: List[(Int, Int)] = Nil) : List[(Int, Int)] = + (xs, acc) match { + case (Nil, acc) => acc.reverse + case (x::xs, Nil) => cnt(xs, (x, 1)::Nil) + case (x::xs, (y, n)::ys) => + if (x == y) cnt(xs, (y, n + 1)::ys) + else cnt(xs, (x, 1)::(y, n)::ys) + } + +def toStr(p: (Int, Int)) = + if (p._2 == 1) s"${p._1}" else s"${p._2} x ${p._1}" + +def pp_list(xs: List[Int]) = { + cnt(xs).map(toStr) +} + +pp_list(List(1,1,1,2,3,3)) // List(3 x 1, 2, 2 x 3) +pp_list(List(1,2,3,4,4,4)) // List(1, 2, 3, 3 x 4) +pp_list(List(1,1,1,1,1,1)) // List(6 x 1) +pp_list(List(1,1,1,2,1,1)) // List(3 x 1, 2, 2 x 1) + +// second version with just a simple counter + +def cnt2(xs: List[Int], n : Int = 0) : List[(Int, Int)] = xs match { + case Nil => Nil + case x::Nil => (x, n + 1)::Nil + case x::y::tail => + if (x == y) cnt2(y::tail, n + 1) + else (x, n + 1)::cnt2(y::tail, 0) +} + +def pp_list2(xs: List[Int]) = { + cnt2(xs).map(toStr) +} + +pp_list2(List(1,1,1,2,3,3)) // List(3 x 1, 2, 2 x 3) +pp_list2(List(1,2,3,4,4,4)) // List(1, 2, 3, 3 x 4) +pp_list2(List(1,1,1,1,1,1)) // List(6 x 1) +pp_list2(List(1,1,1,2,1,1)) // List(3 x 1, 2, 2 x 1) \ No newline at end of file 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: