wsheets/wsh01.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 15 Sep 2024 12:57:59 +0100
changeset 493 244df77507c2
parent 444 7a0735db4788
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
     1
% !TEX program = xelatex
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\documentclass{article}
426
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
     3
\usepackage{../styles/style}
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
     4
\usepackage{../styles/langs}
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
     5
\usepackage{tikz}
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
     6
\usepackage{pgf}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
\usepackage{marvosym}
184
84dc794928de updated
pdated
parents: 183
diff changeset
     8
\usepackage{boxedminipage}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
    10
\lstset{escapeinside={/*!}{!*/}}
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
    11
\newcommand{\annotation}[1]{\hfill\footnotesize{}#1}
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
    12
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
    13
\usepackage{menukeys}
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    14
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    15
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    16
% Exact colors from NB
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    17
\usepackage[breakable]{tcolorbox}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    18
\definecolor{incolor}{HTML}{303F9F}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    19
\definecolor{outcolor}{HTML}{D84315}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    20
\definecolor{cellborder}{HTML}{CFCFCF}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    21
\definecolor{cellbackground}{HTML}{F7F7F7}
334
841727e27252 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    22
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    23
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    24
    
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
\begin{document}
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    26
\fnote{\copyright{} Christian Urban, King's College London, 2022}
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
    27
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    28
\section*{Scala Worksheet 1}
188
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
    29
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    30
Please install Scala on your work-machine: You should have
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    31
Scala up and running after this week, and also have an IDE that allows you to
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    32
access the Scala REPL. Some installation instructions are given at
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    33
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    34
\begin{center}
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    35
\url{https://www.scala-lang.org/download/2.13.10.html}
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    36
\end{center}  
395
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    37
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    38
\subsubsection*{MacOSX}
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
    39
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    40
\begin{itemize}
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    41
\item[0)] (if needed) \texttt{brew install java} \;\;or\;\; \texttt{brew reinstall java}
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    42
\item[1)] \texttt{brew install scala@2.13}
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    43
\end{itemize}
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    44
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    45
\noindent Another method to install Scala on MacOSX
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    46
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    47
\begin{itemize} 
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    48
\item[1)] \texttt{curl -s "https://get.sdkman.io" | bash}
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    49
\item[2)] \texttt{sdk install scala 2.13.10}  
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    50
\end{itemize}
441
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
    51
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    52
\subsubsection*{Windows / Linux Ubuntu}
441
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
    53
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    54
\begin{itemize}
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    55
\item[0)] (if needed) \texttt{sudo apt-get remove scala-library scala}
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    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}}
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    57
\item[2)] \texttt{sudo dpkg -i scala-2.13.10.deb}  
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    58
\end{itemize}
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    59
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    60
\noindent
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    61
Other Linux distros: \texttt{sudo apt-get scala}\bigskip
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    62
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    63
\noindent In the end you should have something running like in your terminal/shell:
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
$ scala
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    67
Welcome to Scala 2.13.10 (OpenJDK 64-Bit Server VM, Java 17.0.1).
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
Type in expressions for evaluation. Or try :help.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
scala>
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
\end{lstlisting}%$
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    73
\noindent
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    74
If you want to use VS Code or Codium, you might want to also look into setting up
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    75
a key-shortcut for ``Run‐Selected‐Text‐In‐Active‐Terminal'' and also set up
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    76
syntax highlighting. Have a look at the Scala handout uploaded to KEATS for information.\bigskip
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    77
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    78
\noindent
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    79
Make sure you have checked out the template files for the Scala CWs and registered the
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    80
Github repository on KEATS. See whether you receive a test report when pushing some code.
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    81
\newpage
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    82
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    83
\subsection*{Task 1}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    85
`Play' with the Scala REPL and try out the following queries. Observe
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    86
what Scala responds. Does it make sense?
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
scala> 2 + 2
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
scala> 1 / 2
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
scala> 1.0 / 2
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
scala> 1 / 2.0
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
scala> 1 / 0
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
scala> 1.0 / 0.0
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
scala> true == false
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
scala> true && false
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
scala> 1 > 1.0
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
scala> "12345".length
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    99
scala> List(1,2,1).size
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   100
scala> Set(1,2,1).size
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   101
scala> List(1) == List(1)
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   102
scala> Set(1,2,3) == Set(3,2,1)
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   103
scala> Array(1) == Array(1)
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   104
scala> Array(1).sameElements(Array(1))
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   105
\end{lstlisting}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   106
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   107
\subsection*{Task 2 (Vals)}
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   108
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   109
Type in the value declarations below and observe Scala's responses. Explain what
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   110
is printed as the value for \texttt{x} in the last line. Is \texttt{z} re-assigned
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   111
in the 4th line?
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   112
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   113
\begin{lstlisting}[numbers=none]
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   114
scala> val z = 42
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   115
scala> z = z + 1
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   116
scala> val x = 2 * z
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   117
scala> val z = 466
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   118
scala> println(x)
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   119
\end{lstlisting}
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   120
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   121
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   122
\subsection*{Task 3 (Strings)}
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   123
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   124
Get familiar with constructing strings and printing strings
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   125
(i.e.~\texttt{println}, \texttt{mkString}, \texttt{toString}, \ldots)
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   126
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   127
\begin{lstlisting}[numbers=none]
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   128
scala> println("Hello " ++ "World")
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   129
scala> List(1,2,3,4).mkString("\n")
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   130
scala> List(1,2,3,4).mkString("(", "|", ")")
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   133
\subsection*{Task 4 (Functions)}
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   134
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   135
Write a function \texttt{miles2meters} taking an integer as argument
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   136
and generating an integer. You can assume that one mile is 1609 meters.
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   137
Remember that functions in Scala do not use the \texttt{return}-keyword.
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   138
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   139
\subsection*{Task 5 (If-Conditions)}
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   140
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   141
Make sure you understand if-conditions in Scala: They are
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   142
\emph{expressions}, meaning they need to calculate a result. For
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   143
example an \texttt{if} without an else-branch is nearly always
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   144
\emph{not} what you intend to write (because you are not meant to
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   145
change any ``state'' outside the expression). Also, remember the quirks
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   146
in Scala with if-conditions needing parentheses and there is no
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   147
\texttt{then}-keyword.
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   148
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   149
\begin{lstlisting}[numbers=none]
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   150
scala> val s1 = "foo"
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   151
scala> val s2 = "bar"
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   152
scala  val s3 = "foobar"
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   153
scala> if (s1 == s2) print("equal") else print("unequal")
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   154
scala> if (s1 ++ s2 == s3) print("equal") else print("unequal")
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
   155
\end{lstlisting}
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
   156
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   157
\subsection*{Task 6 (For-Comprehensions, Harder)}
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   158
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   159
Write \texttt{for}-comprehensions that enumerate all triples containing the numbers 1 - 5. That is,
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   160
print the list
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   161
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   162
\[
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   163
\texttt{(1,1,1)}, \texttt{(2,1,1)}, \texttt{(3,1,1)} \;\ldots\; \texttt{(5,5,5)}
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   164
\]
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   165
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   166
\noindent
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   167
Modify the \texttt{for}-comprehensions such that only triples are
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   168
printed where the sum is divisible by 3.  Then sort the list according
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   169
to the middle element (for this use \texttt{sortBy}).
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   170
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   171
\subsection*{Task 7 (Recursion, Advanced /Hard)}
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   172
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   173
Write a pretty print function for lists of integers which
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   174
``condenses'' elements in the list, meaning if there is a number
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   175
several times in a row, then print out \mbox{\texttt{n x e}}, where
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   176
\texttt{n} is the number of repetitions and \texttt{e} is the
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   177
number. For example
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   178
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   179
\begin{lstlisting}[numbers=none]
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   180
  List(1,1,1,2,3,3)    =>   List(3 x 1, 2, 2 x 3)
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   181
  List(1,2,3,4,4,4)    =>   List(1, 2, 3, 3 x 4)
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   182
  List(1,1,1,1,1,1)    =>   List(6 x 1)
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   183
  List(1,1,1,2,1,1)    =>   List(3 x 1, 2, 2 x 1)
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   184
\end{lstlisting}
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   185
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   186
You might like to define a separate function that first counts the occurences
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   187
for each element and returns a list of (Int, Int)-pairs .
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   188
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
\end{document}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
%%% Local Variables: 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
%%% mode: latex
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
%%% TeX-master: t
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
%%% End: