wsheets/wsh01.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 10 Nov 2022 19:41:04 +0000
changeset 442 c86e7dd198bf
parent 441 handouts/pep-ho.tex@3bfe81972674
child 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
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    31
Scala up and running, and also an IDE that allows you to
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    32
access the Scala REPL. Some 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}
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
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.7.deb}}
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
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    63
\noindent In the end you should have something running like
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
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    73
\newpage
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    74
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    75
\subsection*{Task 1}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    77
`Play' with the Scala REPL and try out the following querries. Observe
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    78
what Scala responds.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
scala> 2 + 2
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
scala> 1 / 2
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
scala> 1.0 / 2
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
scala> 1 / 2.0
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
scala> 1 / 0
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
scala> 1.0 / 0.0
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
scala> true == false
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
scala> true && false
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
scala> 1 > 1.0
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
scala> "12345".length
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    91
scala> List(1,2,1).size
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    92
scala> Set(1,2,1).size
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
    93
scala> List(1) == List(1)
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    94
scala> Set(1,2,3) == Set(3,2,1)
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
    95
scala> Array(1) == Array(1)
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
    96
scala> Array(1).sameElements(Array(1))
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    97
\end{lstlisting}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    98
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    99
\subsection*{Task 2 (Vals)}
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   100
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   101
scala> val z = 42
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   102
scala> z = z + 1
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   103
scala> val z = 466
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   104
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   105
\subsection*{Task 3}
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   106
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
   107
\begin{lstlisting}[numbers=none]
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   108
scala> println("Hello " ++ "World")
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   109
scala> 
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   112
\begin{lstlisting}[numbers=none]
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   113
scala> println("Hello " ++ "World")
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
   114
scala> 
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
   115
\end{lstlisting}
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
   116
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
\end{document}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
%%% Local Variables: 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
%%% mode: latex
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
%%% TeX-master: t
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
%%% End: