197
|
1 |
% !TEX program = xelatex
|
123
|
2 |
\documentclass{article}
|
426
|
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}
|
442
|
26 |
\fnote{\copyright{} Christian Urban, King's College London, 2022}
|
195
|
27 |
|
442
|
28 |
\section*{Scala Worksheet 1}
|
188
|
29 |
|
442
|
30 |
Please install Scala on your work-machine: You should have
|
|
31 |
Scala up and running, and also an IDE that allows you to
|
|
32 |
access the Scala REPL. Some instructions are given at
|
170
|
33 |
|
442
|
34 |
\begin{center}
|
|
35 |
\url{https://www.scala-lang.org/download/2.13.10.html}
|
|
36 |
\end{center}
|
395
|
37 |
|
442
|
38 |
\subsubsection*{MacOSX}
|
195
|
39 |
|
442
|
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 |
|
442
|
45 |
\noindent Another method to install Scala on MacOSX
|
181
|
46 |
|
442
|
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}
|
441
|
51 |
|
442
|
52 |
\subsubsection*{Windows / Linux Ubuntu}
|
441
|
53 |
|
442
|
54 |
\begin{itemize}
|
|
55 |
\item[0)] (if needed) \texttt{sudo apt-get remove scala-library scala}
|
|
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}}
|
|
57 |
\item[2)] \texttt{sudo dpkg -i scala-2.13.10.deb}
|
|
58 |
\end{itemize}
|
170
|
59 |
|
|
60 |
\noindent
|
442
|
61 |
Other Linux distros: \texttt{sudo apt-get scala}\bigskip
|
182
|
62 |
|
442
|
63 |
\noindent In the end you should have something running like
|
123
|
64 |
|
|
65 |
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
|
|
66 |
$ scala
|
442
|
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 |
|
442
|
73 |
\newpage
|
181
|
74 |
|
442
|
75 |
\subsection*{Task 1}
|
123
|
76 |
|
442
|
77 |
`Play' with the Scala REPL and try out the following querries. Observe
|
|
78 |
what Scala responds.
|
123
|
79 |
|
|
80 |
\begin{lstlisting}[numbers=none]
|
|
81 |
scala> 2 + 2
|
|
82 |
scala> 1 / 2
|
|
83 |
scala> 1.0 / 2
|
|
84 |
scala> 1 / 2.0
|
|
85 |
scala> 1 / 0
|
|
86 |
scala> 1.0 / 0.0
|
|
87 |
scala> true == false
|
|
88 |
scala> true && false
|
|
89 |
scala> 1 > 1.0
|
|
90 |
scala> "12345".length
|
181
|
91 |
scala> List(1,2,1).size
|
|
92 |
scala> Set(1,2,1).size
|
265
|
93 |
scala> List(1) == List(1)
|
442
|
94 |
scala> Set(1,2,3) == Set(3,2,1)
|
265
|
95 |
scala> Array(1) == Array(1)
|
|
96 |
scala> Array(1).sameElements(Array(1))
|
335
|
97 |
\end{lstlisting}
|
|
98 |
|
442
|
99 |
\subsection*{Task 2 (Vals)}
|
272
|
100 |
|
442
|
101 |
scala> val z = 42
|
|
102 |
scala> z = z + 1
|
|
103 |
scala> val z = 466
|
271
|
104 |
|
442
|
105 |
\subsection*{Task 3}
|
301
|
106 |
|
|
107 |
\begin{lstlisting}[numbers=none]
|
442
|
108 |
scala> println("Hello " ++ "World")
|
|
109 |
scala>
|
123
|
110 |
\end{lstlisting}
|
|
111 |
|
442
|
112 |
\begin{lstlisting}[numbers=none]
|
|
113 |
scala> println("Hello " ++ "World")
|
|
114 |
scala>
|
343
|
115 |
\end{lstlisting}
|
|
116 |
|
123
|
117 |
\end{document}
|
|
118 |
|
|
119 |
%%% Local Variables:
|
|
120 |
%%% mode: latex
|
|
121 |
%%% TeX-master: t
|
|
122 |
%%% End:
|