author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Thu, 13 Oct 2016 13:13:27 +0100 | |
changeset 450 | d91a1748a9be |
parent 430 | e0492fe3d10b |
child 471 | 9476086849ad |
permissions | -rw-r--r-- |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
236
34e901c529ce
farmed out style files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
235
diff
changeset
|
2 |
\usepackage{../style} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
\usepackage{../langs} |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
4 |
\usepackage{marvosym} |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
5 |
|
402
55f097ab96c9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
398
diff
changeset
|
6 |
%cheat sheet |
55f097ab96c9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
398
diff
changeset
|
7 |
%http://worldline.github.io/scala-cheatsheet/ |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
\begin{document} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
\section*{A Crash-Course on Scala} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
13 |
Scala is a programming language that combines functional and |
405
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
14 |
object-oriented programming-styles. It has received quite a |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
15 |
bit of attention in the last five years or so. One reason for |
232
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
16 |
this attention is that, like the Java programming language, |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
17 |
Scala compiles to the Java Virtual Machine (JVM) and therefore |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
18 |
Scala programs can run under MacOSX, Linux and |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
19 |
Windows.\footnote{There are also experimental backends for |
405
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
20 |
Android and JavaScript; and also work is under way to have a |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
21 |
native compiler, see \url{https://github.com/scala-native/scala-native}.} Unlike Java, however, Scala often |
232
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
22 |
allows programmers to write very concise and elegant code. |
405
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
23 |
Some therefore say: Scala is the much better Java. A number of |
239
68d98140b90b
added notation handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
24 |
companies, The Guardian, Twitter, Coursera, FourSquare, |
68d98140b90b
added notation handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
25 |
LinkedIn to name a few, either use Scala exclusively in |
405
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
26 |
production code, or at least to some substantial degree. It |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
27 |
also seems to be useful in job-interviews (in Data Science) |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
28 |
according to this annectotical report |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
29 |
|
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
30 |
\begin{quote} |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
31 |
\url{https://techcrunch.com/2016/06/14/scala-is-the-new-golden-child/} |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
32 |
\end{quote} |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
33 |
|
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
34 |
\noindent |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
35 |
If you want to try out Scala yourself, the official Scala compiler can be |
239
68d98140b90b
added notation handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
238
diff
changeset
|
36 |
downloaded from |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
\begin{quote} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
\url{http://www.scala-lang.org} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
\end{quote} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
|
430
e0492fe3d10b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
405
diff
changeset
|
42 |
\noindent |
e0492fe3d10b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
405
diff
changeset
|
43 |
A ready-mad bundle with the Eclipse IDE is at |
e0492fe3d10b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
405
diff
changeset
|
44 |
|
e0492fe3d10b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
405
diff
changeset
|
45 |
\begin{quote} |
e0492fe3d10b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
405
diff
changeset
|
46 |
\url{http://scala-ide.org/download/sdk.html} |
e0492fe3d10b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
405
diff
changeset
|
47 |
\end{quote} |
e0492fe3d10b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
405
diff
changeset
|
48 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
49 |
Why do I use Scala in the AFL module? Actually, you can do |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
50 |
\emph{any} part of the coursework in \emph{any} programming |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
51 |
language you like. I use Scala for showing you code during the |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
52 |
lectures because its functional programming-style allows me to |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
53 |
implement the functions we will discuss with very small |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
54 |
code-snippets. If I had to do this in Java, I would first have |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
55 |
to go through heaps of boilerplate code and the code-snippets |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
56 |
would not look pretty. Since the Scala compiler is free, you |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
57 |
can download the code-snippets and run every example I give. |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
58 |
But if you prefer, you can also easily translate them into any |
405
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
59 |
other functional language, for example Haskell, Swift, |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
60 |
Standard ML, F$^\#$, Ocaml and so on. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
62 |
Developing programs in Scala can be done with the Eclipse IDE |
405
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
63 |
and also with the IntelliJ IDE, but for the small programs I will |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
64 |
develop the good old Emacs-editor is adequate for me and I |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
65 |
will run the programs on the command line. One advantage of |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
66 |
Scala over Java is that it includes an interpreter (a REPL, or |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
67 |
\underline{R}ead-\underline{E}val-\underline{P}rint-\underline{L}oop) |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
68 |
with which you can run and test small code-snippets without |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
69 |
the need of the compiler. This helps a lot with interactively |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
70 |
developing programs. Once you installed Scala, you can start |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
71 |
the interpreter by typing on the command line: |
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
72 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
73 |
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
74 |
$ scala |
398
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
75 |
Welcome to Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM). |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
76 |
Type in expressions for evaluation. Or try :help. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
scala> |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
79 |
\end{lstlisting} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
|
405
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
81 |
\noindent Of course the precise response may vary due to the |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
82 |
version and platform where you installed Scala. At the Scala |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
83 |
prompt you can type things like \code{2 + 3} \keys{Ret} and |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
84 |
the output will be |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
86 |
\begin{lstlisting}[numbers=none] |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
scala> 2 + 3 |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
res0: Int = 5 |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
89 |
\end{lstlisting} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
91 |
\noindent indicating that the result of the addition is of |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
92 |
type \code{Int} and the actual result is 5. Another classic |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
93 |
example you can try out is |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
95 |
\begin{lstlisting}[numbers=none] |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
96 |
scala> print("hello world") |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
hello world |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
98 |
\end{lstlisting} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
100 |
\noindent Note that in this case there is no result. The |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
101 |
reason is that \code{print} does not actually produce a result |
235
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
102 |
(there is no \code{resXX} and no type), rather it is a |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
103 |
function that causes the \emph{side-effect} of printing out a |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
104 |
string. Once you are more familiar with the functional |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
105 |
programming-style, you will know what the difference is |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
106 |
between a function that returns a result, like addition, and a |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
107 |
function that causes a side-effect, like \code{print}. We |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
108 |
shall come back to this point later, but if you are curious |
245
a5fade10c207
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
109 |
now, the latter kind of functions always has \code{Unit} as |
a5fade10c207
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
110 |
return type. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
112 |
If you want to write a stand-alone app in Scala, you can |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
113 |
implement an object that is an instance of \code{App}, say |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
114 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
115 |
\begin{lstlisting}[numbers=none] |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
116 |
object Hello extends App { |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
117 |
println("hello world") |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
118 |
} |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
119 |
\end{lstlisting} |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
120 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
121 |
\noindent save it in a file, say {\tt hello-world.scala}, and |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
122 |
then run the compiler and runtime environment: |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
123 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
124 |
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
125 |
$ scalac hello-world.scala |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
126 |
$ scala Hello |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
127 |
hello world |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
128 |
\end{lstlisting} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
129 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
130 |
As mentioned above, Scala targets the JVM and consequently |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
131 |
Scala programs can also be executed by the bog-standard Java |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
132 |
Runtime. This only requires the inclusion of {\tt |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
133 |
scala-library.jar}, which on my computer can be done as |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
134 |
follows: |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
135 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
136 |
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
137 |
$ scalac hello-world.scala |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
138 |
$ java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
139 |
hello world |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
140 |
\end{lstlisting} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
141 |
|
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
142 |
\noindent You might need to adapt the path to where you have |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
143 |
installed Scala. |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
144 |
|
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
145 |
\subsection*{Inductive Datatypes} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
146 |
|
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
147 |
The elegance and conciseness of Scala programs are often a |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
148 |
result of inductive datatypes that can be easily defined in |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
149 |
Scala. For example in ``every-day mathematics'' we define |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
150 |
regular expressions simply by giving the grammar |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
151 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
152 |
\begin{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
153 |
\begin{tabular}{r@{\hspace{2mm}}r@{\hspace{2mm}}l@{\hspace{13mm}}l} |
398
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
154 |
$r$ & $::=$ & $\ZERO$ & null\\ |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
155 |
& $\mid$ & $\ONE$ & empty string\\ |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
156 |
& $\mid$ & $c$ & single character\\ |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
157 |
& $\mid$ & $r_1 \cdot r_2$ & sequence\\ |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
158 |
& $\mid$ & $r_1 + r_2$ & alternative / choice\\ |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
159 |
& $\mid$ & $r^\star$ & star (zero or more)\\ |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
160 |
\end{tabular} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
161 |
\end{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
162 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
163 |
\noindent This grammar specifies what regular expressions are |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
164 |
(essentially a kind of tree-structure with three kinds of |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
165 |
inner nodes---sequence, alternative and star---and three kinds |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
166 |
of leave nodes---null, empty and character). If you are |
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
167 |
familiar with Java, it might be an instructive exercise to |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
168 |
define this kind of inductive datatypes in Java\footnote{Happy |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
169 |
programming! \Smiley} and then compare it with how it can be |
245
a5fade10c207
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
242
diff
changeset
|
170 |
implemented in Scala. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
171 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
172 |
Implementing the regular expressions from above in Scala is |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
173 |
actually very simple: It first requires an \emph{abstract |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
174 |
class}, say, \code{Rexp}. This will act as the type for |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
175 |
regular expressions. Second, it requires a case for each |
398
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
176 |
clause in the grammar. The cases for $\ZERO$ and $\ONE$ do not |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
177 |
have any arguments, while in all the other cases we do have |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
178 |
arguments. For example the character regular expression needs |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
179 |
to take as an argument the character it is supposed to |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
180 |
recognise. In Scala, the cases without arguments are called |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
181 |
\emph{case objects}, whereas the ones with arguments are |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
182 |
\emph{case classes}. The corresponding Scala code is as |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
183 |
follows: |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
184 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
185 |
\begin{lstlisting}[numbers=none] |
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
186 |
abstract class Rexp |
398
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
187 |
case object ZERO extends Rexp |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
188 |
case object ONE extends Rexp |
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
189 |
case class CHAR (c: Char) extends Rexp |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
190 |
case class SEQ (r1: Rexp, r2: Rexp) extends Rexp |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
191 |
case class ALT (r1: Rexp, r2: Rexp) extends Rexp |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
192 |
case class STAR (r: Rexp) extends Rexp |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
193 |
\end{lstlisting} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
194 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
195 |
\noindent In order to be an instance of \code{Rexp}, each case |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
196 |
object and case class needs to extend \code{Rexp}. Given the |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
197 |
grammar above, I hope you can see the underlying pattern. If |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
198 |
you want to play further with such definitions of inductive |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
199 |
datatypes, feel free to define for example binary trees. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
200 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
201 |
Once you make a definition like the one above in Scala, you |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
202 |
can represent the regular expression for $a + b$, for example, |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
203 |
as \code{ALT(CHAR('a'), CHAR('b'))}. Expressions such as |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
204 |
\code{'a'} stand for ASCII characters, though in the output |
235
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
205 |
syntax, as you can see below, the quotes are omitted. In a |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
206 |
later section we will see how we can support the more |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
207 |
mathematical infix notation for regular expression operators |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
208 |
in Scala. If you want to assign this regular expression to a |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
209 |
variable, you can use the keyword \code{val} and type |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
210 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
211 |
\begin{lstlisting}[numbers=none] |
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
212 |
scala> val r = ALT(CHAR('a'), CHAR('b')) |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
213 |
r: ALT = ALT(CHAR(a),CHAR(b)) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
214 |
\end{lstlisting} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
215 |
|
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
216 |
\noindent As you can see, in order to make such assignments, |
405
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
217 |
no \code{new} or constructor is required in the class (as in |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
218 |
Java). However, if there is the need for some non-standard |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
219 |
initialisation, you can of course define such a constructor in |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
220 |
Scala too. But we omit such ``tricks'' here. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
221 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
222 |
Note that Scala in its response says the variable \code{r} is |
398
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
223 |
of type \code{ALT}, not \code{Rexp}. This might be a bit |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
224 |
unexpected, but can be explained as follows: Scala always |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
225 |
tries to find the most general type that is needed for a |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
226 |
variable or expression, but does not ``over-generalise''. In |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
227 |
our definition the type \code{Rexp} is more general than |
405
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
228 |
\code{ALT}, since it is the abstract class for all regular |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
229 |
expressions. But in this particular case there is no need to |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
230 |
give \code{r} the more general type of \code{Rexp}. This is |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
231 |
different if you want to form a list of regular expressions, |
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
232 |
for example |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
233 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
234 |
\begin{lstlisting}[numbers=none] |
398
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
235 |
scala> val ls = List(ALT(CHAR('a'), CHAR('b')), ZERO) |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
236 |
ls: List[Rexp] = List(ALT(CHAR(a),CHAR(b)), ZERO) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
237 |
\end{lstlisting} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
238 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
239 |
\noindent In this case, Scala needs to assign a common type to |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
240 |
the regular expressions so that it is compatible with the |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
241 |
fact that lists can only contain elements of a single type. In |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
242 |
this case the first common type is \code{Rexp}.\footnote{If you |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
243 |
type in this example, you will notice that the type contains |
315
470922b46a63
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
247
diff
changeset
|
244 |
some further information, but let us ignore this for the |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
245 |
moment.} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
246 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
247 |
For compound types like \code{List[...]}, the general rule is |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
248 |
that when a type takes another type as argument, then this |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
249 |
argument type is written in angle-brackets. This can also |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
250 |
contain nested types as in \code{List[Set[Rexp]]}, which is a |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
251 |
list of sets each of which contains regular expressions. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
252 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
253 |
\subsection*{Functions and Pattern-Matching} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
254 |
|
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
255 |
I mentioned above that Scala is a very elegant programming |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
256 |
language for the code we will write in this module. This |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
257 |
elegance mainly stems from the fact that in addition to |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
258 |
inductive datatypes, also functions can be implemented very |
315
470922b46a63
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
247
diff
changeset
|
259 |
easily in Scala. To show you this, let us first consider a |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
260 |
problem from number theory, called the \emph{Collatz-series}, |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
261 |
which corresponds to a famous unsolved problem in |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
262 |
mathematics.\footnote{See for example |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
263 |
\url{http://mathworld.wolfram.com/CollatzProblem.html}.} |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
264 |
Mathematicians define this series as: |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
265 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
266 |
\[ |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
267 |
collatz_{n + 1} \dn |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
268 |
\begin{cases} |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
269 |
\frac{1}{2} * collatz_n & \text{if $collatz_n$ is even}\\ |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
270 |
3 * collatz_n + 1 & \text{if $collatz_n$ is odd} |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
271 |
\end{cases} |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
272 |
\] |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
273 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
274 |
\noindent The famous unsolved question is whether this |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
275 |
series started with any $n > 0$ as $collatz_0$ will always |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
276 |
return to $1$. This is obvious when started with $1$, and also |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
277 |
with $2$, but already needs a bit of head-scratching for the |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
278 |
case of $3$. |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
279 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
280 |
If we want to avoid the head-scratching, we could implement |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
281 |
this as the following function in Scala: |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
282 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
283 |
\lstinputlisting[numbers=none]{../progs/collatz.scala} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
284 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
285 |
\noindent The keyword for function definitions is \code{def} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
286 |
followed by the name of the function. After that you have a |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
287 |
list of arguments (enclosed in parentheses and separated by |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
288 |
commas). Each argument in this list needs its type to be |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
289 |
annotated. In this case we only have one argument, which is of |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
290 |
type \code{BigInt}. This type stands in Scala for arbitrary |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
291 |
precision integers (in case you want to try out the function |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
292 |
on really big numbers). After the arguments comes the type of |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
293 |
what the function returns---a Boolean in this case for |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
294 |
indicating that the function has reached 1. Finally, after the |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
295 |
\code{=} comes the \emph{body} of the function implementing |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
296 |
what the function is supposed to do. What the \code{collatz} |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
297 |
function does should be pretty self-explanatory: the function |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
298 |
first tests whether \code{n} is equal to 1 in which case it |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
299 |
returns \code{true} and so on. |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
300 |
|
247
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
301 |
Notice the quirk in Scala's syntax for \code{if}s: The condition |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
302 |
needs to be enclosed in parentheses and the then-case comes |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
303 |
right after the condition---there is no \code{then} keyword in |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
304 |
Scala. |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
305 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
306 |
The real power of Scala comes, however, from the ability to |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
307 |
define functions by \emph{pattern matching}. In the |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
308 |
\code{collatz} function above we need to test each case using a |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
309 |
sequence of \code{if}s. This can be very cumbersome and brittle |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
310 |
if there are many cases. If we wanted to define a function |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
311 |
over regular expressions in Java, for example, which does not |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
312 |
have pattern-matching, the resulting code would just be |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
313 |
awkward. |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
314 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
315 |
Mathematicians already use the power of pattern-matching when |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
316 |
they define the function that takes a regular expression and |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
317 |
produces another regular expression that can recognise the |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
318 |
reversed strings. They define this function as follows: |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
319 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
320 |
\begin{center} |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
321 |
\begin{tabular}{r@{\hspace{2mm}}c@{\hspace{2mm}}l} |
398
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
322 |
$rev(\ZERO)$ & $\dn$ & $\ZERO$\\ |
c8ce95067c1a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
397
diff
changeset
|
323 |
$rev(\ONE)$ & $\dn$ & $\ONE$\\ |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
324 |
$rev(c)$ & $\dn$ & $c$\\ |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
325 |
$rev(r_1 + r_2)$ & $\dn$ & $rev(r_1) + rev(r_2)$\\ |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
326 |
$rev(r_1 \cdot r_2)$ & $\dn$ & $rev(r_2) \cdot rev(r_1)$\\ |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
327 |
$rev(r^*)$ & $\dn$ & $rev(r)^*$\\ |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
328 |
\end{tabular} |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
329 |
\end{center} |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
330 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
331 |
\noindent It is defined by recursion analysing each pattern of |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
332 |
what the regular expression might look like. The corresponding |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
333 |
Scala code looks very similar to this definition, thanks to |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
334 |
pattern-matching. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
335 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
336 |
\lstinputlisting[language=Scala]{../progs/rev.scala} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
337 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
338 |
\noindent The keyword for starting a pattern-match is |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
339 |
\code{match} followed by a list of \code{case}s. Before the |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
340 |
match keyword can be another pattern, but often, as in the |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
341 |
case above, it is just a variable you want to pattern-match |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
342 |
(the \code{r} after \code{=} in Line 1). |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
343 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
344 |
Each case in this definition follows the structure of how we |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
345 |
defined regular expressions as inductive datatype. For example |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
346 |
the case in Line 3 you can read as: if the regular expression |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
347 |
\code{r} is of the form \code{EMPTY} then do whatever follows |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
348 |
the \code{=>} (in this case just return \code{EMPTY}). Line 5 |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
349 |
reads as: if the regular expression \code{r} is of the form |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
350 |
\code{ALT(r1, r2)}, where the left-branch of the alternative is |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
351 |
matched by the variable \code{r1} and the right-branch by |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
352 |
\code{r2} then do ``something''. The ``something'' can now use the |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
353 |
variables \code{r1} and \code{r2} from the match. |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
354 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
355 |
If you want to play with this function, call it for example |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
356 |
with the regular expression $ab + ac$. This regular expression |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
357 |
can recognise the strings $ab$ and $ac$. The function |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
358 |
\code{rev} produces $ba + ca$, which can recognise the reversed |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
359 |
strings $ba$ and $ca$. |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
360 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
361 |
In Scala each pattern-match can also be guarded as in |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
362 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
363 |
\begin{lstlisting}[ numbers=none] |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
364 |
case Pattern if Condition => Do_Something |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
365 |
\end{lstlisting} |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
366 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
367 |
\noindent This allows us, for example, to re-write the |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
368 |
\code{collatz}-function from above as follows: |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
369 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
370 |
\lstinputlisting[language=Scala]{../progs/collatz2.scala} |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
371 |
|
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
372 |
|
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
373 |
\noindent Although in this particular case the pattern-match |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
374 |
does not improve the code in any way. In cases like |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
375 |
\code{rev}, however, it is really crucial. The underscore in |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
376 |
Line 4 indicates that we do not care what the pattern looks |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
377 |
like. Thus this case acts like a default case whenever the |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
378 |
cases above did not match. Cases are always tried out from top |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
379 |
to bottom. |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
380 |
|
405
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
381 |
\subsection*{Loops, or better the Absence thereof} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
382 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
383 |
Coming from Java or C, you might be surprised that Scala does |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
384 |
not really have loops. It has instead, what is in functional |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
240
diff
changeset
|
385 |
programming called, \emph{maps}. To illustrate how they work, |
315
470922b46a63
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
247
diff
changeset
|
386 |
let us assume you have a list of numbers from 1 to 8 and want to |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
387 |
build the list of squares. The list of numbers from 1 to 8 |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
388 |
can be constructed in Scala as follows: |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
389 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
390 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
391 |
scala> (1 to 8).toList |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
392 |
res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
393 |
\end{lstlisting} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
394 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
395 |
\noindent Generating from this list, the list of squares in a |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
396 |
programming language such as Java, you would assume the list |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
397 |
is given as a kind of array. You would then iterate, or loop, |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
398 |
an index over this array and replace each entry in the array |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
399 |
by the square. Right? In Scala, and in other functional |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
400 |
programming languages, you use maps to achieve the same. |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
401 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
402 |
A map essentially takes a function that describes how each |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
403 |
element is transformed (for example squared) and a list over |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
404 |
which this function should work. There are two forms to |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
405 |
express such maps in Scala. The first way is called a |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
406 |
\emph{for-comprehension}. Squaring the numbers from 1 to 8 |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
407 |
would look in this form as follows: |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
408 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
409 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
410 |
scala> for (n <- (1 to 8).toList) yield n * n |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
411 |
res2: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
412 |
\end{lstlisting} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
413 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
414 |
\noindent The important keywords are \code{for} and |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
415 |
\code{yield}. This for-comprehension roughly states that from |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
416 |
the list of numbers we draw \code{n}s and compute the result |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
417 |
of \code{n * n}. As you can see, we specified the list where |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
418 |
each \code{n} comes from, namely \code{(1 to 8).toList}, and |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
419 |
how each element needs to be transformed. This can also be |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
420 |
expressed in a second way in Scala by using directly |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
421 |
\code{map}s as follows: |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
422 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
423 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
424 |
scala> (1 to 8).toList.map(n => n * n) |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
425 |
res3 = List(1, 4, 9, 16, 25, 36, 49, 64) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
426 |
\end{lstlisting} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
427 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
428 |
\noindent In this way, the expression \code{n => n * n} stands |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
429 |
for the function that calculates the square (this is how the |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
430 |
\code{n}s are transformed). This expression for functions |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
431 |
might remind you of your lessons about the lambda-calculus |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
432 |
where this would have been written as $\lambda n.\,n * n$. It |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
433 |
might not be obvious, but for-comprehensions are just |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
434 |
syntactic sugar: when compiling, Scala translates |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
435 |
for-comprehensions into equivalent maps. This even works |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
436 |
when for-comprehensions get more complicated (see below). |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
437 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
438 |
The very charming feature of Scala is that such maps or |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
439 |
for-comprehensions can be written for any kind of data |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
440 |
collection, such as lists, sets, vectors, options and so on. |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
441 |
For example if we instead compute the reminders modulo 3 of |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
442 |
this list, we can write |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
443 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
444 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
445 |
scala> (1 to 8).toList.map(n => n % 3) |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
446 |
res4 = List(1, 2, 0, 1, 2, 0, 1, 2) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
447 |
\end{lstlisting} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
448 |
|
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
449 |
\noindent If we, however, transform the numbers 1 to 8 not |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
450 |
into a list, but into a set, and then compute the reminders |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
451 |
modulo 3 we obtain |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
452 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
453 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
454 |
scala> (1 to 8).toSet[Int].map(n => n % 3) |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
455 |
res5 = Set(2, 1, 0) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
456 |
\end{lstlisting} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
457 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
458 |
\noindent This is the correct result for sets, as there are |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
459 |
only three equivalence classes of integers modulo 3. Note that |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
460 |
in this example we need to ``help'' Scala to transform the |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
461 |
numbers into a set of integers by explicitly annotating the |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
462 |
type \code{Int}. Since maps and for-comprehensions are |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
463 |
just syntactic variants of each other, the latter can also be |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
464 |
written as |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
465 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
466 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
467 |
scala> for (n <- (1 to 8).toSet[Int]) yield n % 3 |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
468 |
res5 = Set(2, 1, 0) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
469 |
\end{lstlisting} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
470 |
|
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
471 |
For-comprehensions can also be nested and the selection of |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
472 |
elements can be guarded. For example if we want to pair up |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
473 |
the numbers 1 to 4 with the letters a to c, we can write |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
474 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
475 |
\begin{lstlisting}[numbers=none] |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
476 |
scala> for (n <- (1 to 4).toList; |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
477 |
m <- ('a' to 'c').toList) yield (n, m) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
478 |
res6 = List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
479 |
(3,a), (3,b), (3,c), (4,a), (4,b), (4,c)) |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
480 |
\end{lstlisting} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
481 |
|
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
482 |
\noindent |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
483 |
Or if we want to find all pairs of numbers between 1 and 3 |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
484 |
where the sum is an even number, we can write |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
485 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
486 |
\begin{lstlisting}[numbers=none] |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
487 |
scala> for (n <- (1 to 3).toList; |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
488 |
m <- (1 to 3).toList; |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
489 |
if (n + m) % 2 == 0) yield (n, m) |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
490 |
res7 = List((1,1), (1,3), (2,2), (3,1), (3,3)) |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
491 |
\end{lstlisting} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
492 |
|
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
493 |
\noindent The \code{if}-condition in the for-comprehension |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
494 |
filters out all pairs where the sum is not even. |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
495 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
496 |
While hopefully this all looks reasonable, there is one |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
497 |
complication: In the examples above we always wanted to |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
498 |
transform one list into another list (e.g.~list of squares), |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
499 |
or one set into another set (set of numbers into set of |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
500 |
reminders modulo 3). What happens if we just want to print out |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
501 |
a list of integers? Then actually the for-comprehension |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
502 |
needs to be modified. The reason is that \code{print}, you |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
503 |
guessed it, does not produce any result, but only produces |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
504 |
what is in the functional-programming-lingo called a |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
505 |
side-effect. Printing out the list of numbers from 1 to 5 |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
506 |
would look as follows |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
507 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
508 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
509 |
scala> for (n <- (1 to 5).toList) print(n) |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
510 |
12345 |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
511 |
\end{lstlisting} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
512 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
513 |
\noindent |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
514 |
where you need to omit the keyword \code{yield}. You can |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
515 |
also do more elaborate calculations such as |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
516 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
517 |
\begin{lstlisting}[numbers=none] |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
518 |
scala> for (n <- (1 to 5).toList) { |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
519 |
val square_n = n * n |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
520 |
println(s"$n * $n = $square_n") |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
521 |
} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
522 |
1 * 1 = 1 |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
523 |
2 * 2 = 4 |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
524 |
3 * 3 = 9 |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
525 |
4 * 4 = 16 |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
526 |
5 * 5 = 25 |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
527 |
\end{lstlisting} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
528 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
529 |
\noindent In this code I use a variable assignment (\code{val |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
530 |
square_n = ...} ) and also what is called in Scala a |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
531 |
\emph{string interpolation}, written \code{s"..."}. The latter |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
532 |
is for printing out an equation. It allows me to refer to the |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
533 |
integer values \code{n} and \code{square\_n} inside a string. |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
534 |
This is very convenient for printing out ``things''. |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
535 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
536 |
The corresponding map construction for functions with |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
537 |
side-effects is in Scala called \code{foreach}. So you |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
538 |
could also write |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
539 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
540 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
541 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
542 |
scala> (1 to 5).toList.foreach(n => print(n)) |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
543 |
12345 |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
544 |
\end{lstlisting} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
545 |
|
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
546 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
547 |
\noindent or even just |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
548 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
549 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
550 |
scala> (1 to 5).toList.foreach(print) |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
551 |
12345 |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
552 |
\end{lstlisting} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
553 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
554 |
\noindent Again I hope this reminds you a bit of your |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
555 |
lambda-calculus lessons, where an explanation is given why |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
556 |
both forms produce the same result. |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
557 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
558 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
559 |
If you want to find out more about maps and functions with |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
560 |
side-effects, you can ponder about the response Scala gives if |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
561 |
you replace \code{foreach} by \code{map} in the expression |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
562 |
above. Scala will still allow \code{map} with side-effect |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
563 |
functions, but then reacts with a slightly interesting result. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
564 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
565 |
\subsection*{Types} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
566 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
567 |
In most functional programming languages, types play an |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
568 |
important role. Scala is such a language. You have already |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
569 |
seen built-in types, like \code{Int}, \code{Boolean}, |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
570 |
\code{String} and \code{BigInt}, but also user-defined ones, |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
571 |
like \code{Rexp}. Unfortunately, types can be a thorny |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
572 |
subject, especially in Scala. For example, why do we need to |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
573 |
give the type to \code{toSet[Int]}, but not to \code{toList}? |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
574 |
The reason is the power of Scala, which sometimes means it |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
575 |
cannot infer all necessary typing information. At the |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
576 |
beginning while getting familiar with Scala, I recommend a |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
577 |
``play-it-by-ear-approach'' to types. Fully understanding |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
578 |
type-systems, especially complicated ones like in Scala, can |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
579 |
take a module on their own.\footnote{Still, such a study can |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
580 |
be a rewarding training: If you are in the business of |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
581 |
designing new programming languages, you will not be able to |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
582 |
turn a blind eye to types. They essentially help programmers |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
583 |
to avoid common programming errors and help with maintaining |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
584 |
code.} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
585 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
586 |
In Scala, types are needed whenever you define an inductive |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
587 |
datatype and also whenever you define functions (their |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
588 |
arguments and their results need a type). Base types are types |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
589 |
that do not take any (type)arguments, for example \code{Int} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
590 |
and \code{String}. Compound types take one or more arguments, |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
591 |
which as seen earlier need to be given in angle-brackets, for |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
592 |
example \code{List[Int]} or \code{Set[List[String]]} or |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
593 |
\code{Map[Int, Int]}. |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
594 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
595 |
There are a few special type-constructors that fall outside |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
596 |
this pattern. One is for tuples, where the type is written |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
597 |
with parentheses. For example |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
598 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
599 |
\begin{lstlisting}[ numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
600 |
(Int, Int, String) |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
601 |
\end{lstlisting} |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
602 |
|
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
603 |
\noindent is for a triple (a tuple with three components---two |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
604 |
integers and a string). Tuples are helpful if you want to |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
605 |
define functions with multiple results, say the function |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
606 |
returning the quotient and reminder of two numbers. For this |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
607 |
you might define: |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
608 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
609 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
610 |
\begin{lstlisting}[ numbers=none] |
235
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
611 |
def quo_rem(m: Int, n: Int) : (Int, Int) = (m / n, m % n) |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
612 |
\end{lstlisting} |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
613 |
|
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
614 |
|
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
615 |
\noindent Since this function returns a pair of integers, its |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
616 |
return type needs to be of type \code{(Int, Int)}. |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
617 |
Incidentally, this is also the input type of this function. |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
618 |
Notice this function takes \emph{two} arguments, namely |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
619 |
\code{m} and \code{n}, both of which are integers. They are |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
620 |
``packaged'' in a pair. Consequently the complete type of |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
621 |
\code{quo_rem} is |
237
370c0647a9bf
more material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
622 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
623 |
\begin{lstlisting}[ numbers=none] |
237
370c0647a9bf
more material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
624 |
(Int, Int) => (Int, Int) |
370c0647a9bf
more material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
625 |
\end{lstlisting} |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
626 |
|
247
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
627 |
Another special type-constructor is for functions, written as |
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
628 |
the arrow \code{=>}. For example, the type \code{Int => |
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
629 |
String} is for a function that takes an integer as input |
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
630 |
argument and produces a string as result. A function of this |
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
631 |
type is for instance |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
632 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
633 |
\begin{lstlisting}[numbers=none] |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
634 |
def mk_string(n: Int) : String = n match { |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
635 |
case 0 => "zero" |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
636 |
case 1 => "one" |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
637 |
case 2 => "two" |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
638 |
case _ => "many" |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
639 |
} |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
640 |
\end{lstlisting} |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
641 |
|
247
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
642 |
\noindent It takes an integer as input argument and returns a |
235
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
643 |
string. Unlike other functional programming languages, there |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
644 |
is in Scala no easy way to find out the types of existing |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
645 |
functions, except by looking into the documentation |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
646 |
|
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
647 |
\begin{quote} |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
648 |
\url{http://www.scala-lang.org/api/current/} |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
649 |
\end{quote} |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
650 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
651 |
The function arrow can also be iterated, as in |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
652 |
\code{Int => String => Boolean}. This is the type for a function |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
653 |
taking an integer as first argument and a string as second, |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
654 |
and the result of the function is a boolean. Though silly, a |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
655 |
function of this type would be |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
656 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
657 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
658 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
659 |
def chk_string(n: Int)(s: String) : Boolean = |
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
660 |
mk_string(n) == s |
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
661 |
\end{lstlisting} |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
662 |
|
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
663 |
|
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
664 |
\noindent which checks whether the integer \code{n} |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
665 |
corresponds to the name \code{s} given by the function |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
666 |
\code{mk\_string}. Notice the unusual way of specifying the |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
667 |
arguments of this function: the arguments are given one after |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
668 |
the other, instead of being in a pair (what would be the type |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
669 |
of this function then?). This way of specifying the arguments |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
670 |
can be useful, for example in situations like this |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
671 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
672 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
673 |
scala> List("one", "two", "three", "many").map(chk_string(2)) |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
674 |
res4 = List(false, true, false, false) |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
675 |
|
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
676 |
scala> List("one", "two", "three", "many").map(chk_string(3)) |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
677 |
res5 = List(false, false, false, true) |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
678 |
\end{lstlisting} |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
679 |
|
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
680 |
\noindent In each case we can give to \code{map} a specialised |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
681 |
version of \code{chk_string}---once specialised to 2 and once |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
682 |
to 3. This kind of ``specialising'' a function is called |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
683 |
\emph{partial application}---we have not yet given to this |
247
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
684 |
function all arguments it needs, but only some of them. |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
685 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
686 |
Coming back to the type \code{Int => String => Boolean}. The |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
687 |
rule about such function types is that the right-most type |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
688 |
specifies what the function returns (a boolean in this case). |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
689 |
The types before that specify how many arguments the function |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
690 |
expects and what their type is (in this case two arguments, |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
691 |
one of type \code{Int} and another of type \code{String}). |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
692 |
Given this rule, what kind of function has type |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
693 |
\mbox{\code{(Int => String) => Boolean}}? Well, it returns a |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
694 |
boolean. More interestingly, though, it only takes a single |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
695 |
argument (because of the parentheses). The single argument |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
696 |
happens to be another function (taking an integer as input and |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
697 |
returning a string). Remember that \code{mk_string} is just |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
698 |
such a function. So how can we use it? For this define |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
699 |
the somewhat silly function \code{apply_3}: |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
700 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
701 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
702 |
def apply_3(f: Int => String): Bool = f(3) == "many" |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
703 |
|
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
704 |
scala> apply_3(mk_string) |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
705 |
res6 = true |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
706 |
\end{lstlisting} |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
707 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
708 |
You might ask: Apart from silly functions like above, what is |
247
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
709 |
the point of having functions as input arguments to other |
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
710 |
functions? In Java there is indeed no need of this kind of |
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
711 |
feature: at least in the past it did not allow such |
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
712 |
constructions. I think, the point of Java 8 is to lift this |
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
713 |
restriction. But in all functional programming languages, |
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
714 |
including Scala, it is really essential to allow functions as |
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
715 |
input argument. Above you already seen \code{map} and |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
716 |
\code{foreach} which need this. Consider the functions |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
717 |
\code{print} and \code{println}, which both print out strings, |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
718 |
but the latter adds a line break. You can call \code{foreach} |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
719 |
with either of them and thus changing how, for example, five |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
720 |
numbers are printed. |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
721 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
722 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
723 |
\begin{lstlisting}[numbers=none] |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
724 |
scala> (1 to 5).toList.foreach(print) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
725 |
12345 |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
726 |
scala> (1 to 5).toList.foreach(println) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
727 |
1 |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
728 |
2 |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
729 |
3 |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
730 |
4 |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
731 |
5 |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
732 |
\end{lstlisting} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
733 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
734 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
735 |
\noindent This is actually one of the main design principles |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
736 |
in functional programming. You have generic functions like |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
737 |
\code{map} and \code{foreach} that can traverse data containers, |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
738 |
like lists or sets. They then take a function to specify what |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
739 |
should be done with each element during the traversal. This |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
740 |
requires that the generic traversal functions can cope with |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
741 |
any kind of function (not just functions that, for example, |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
742 |
take as input an integer and produce a string like above). |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
743 |
This means we cannot fix the type of the generic traversal |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
744 |
functions, but have to keep them |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
745 |
\emph{polymorphic}.\footnote{Another interestic topic about |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
746 |
types, but we omit it here for the sake of brevity.} |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
747 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
748 |
There is one more type constructor that is rather special. It |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
749 |
is called \code{Unit}. Recall that \code{Boolean} has two |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
750 |
values, namely \code{true} and \code{false}. This can be used, |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
751 |
for example, to test something and decide whether the test |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
752 |
succeeds or not. In contrast the type \code{Unit} has only a |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
753 |
single value, written \code{()}. This seems like a completely |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
754 |
useless type and return value for a function, but is actually |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
755 |
quite useful. It indicates when the function does not return |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
756 |
any result. The purpose of these functions is to cause |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
757 |
something being written on the screen or written into a file, |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
758 |
for example. This is what is called they cause some effect on |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
759 |
the side, namely a new content displayed on the screen or some |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
760 |
new data in a file. Scala uses the \code{Unit} type to indicate |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
761 |
that a function does not have a result, but potentially causes |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
762 |
some side-effect. Typical examples are the printing functions, |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
763 |
like \code{print}. |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
764 |
|
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
765 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
766 |
\subsection*{Cool Stuff} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
767 |
|
235
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
768 |
The first wow-moment I had with Scala was when I came across |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
769 |
the following code-snippet for reading a web-page. |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
770 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
771 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
772 |
\begin{lstlisting}[ numbers=none] |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
773 |
import io.Source |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
774 |
val url = """http://www.inf.kcl.ac.uk/staff/urbanc/""" |
430
e0492fe3d10b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
405
diff
changeset
|
775 |
Source.fromURL(url)("ISO-8859-1").take(10000).mkString |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
776 |
\end{lstlisting} |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
777 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
778 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
779 |
\noindent These three lines return a string containing the |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
780 |
HTML-code of my webpage. It actually already does something |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
781 |
more sophisticated, namely only returns the first 10000 |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
782 |
characters of a webpage in case it is too large. Why is that |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
783 |
code-snippet of any interest? Well, try implementing |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
784 |
reading-from-a-webpage in Java. I also like the possibility of |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
785 |
triple-quoting strings, which I have only seen in Scala so |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
786 |
far. The idea behind this is that in such a string all |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
787 |
characters are interpreted literally---there are no escaped |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
788 |
characters, like \verb|\n| for newlines. |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
789 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
790 |
My second wow-moment I had with a feature of Scala that other |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
791 |
functional programming languages do not have. This feature is |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
792 |
about implicit type conversions. If you have regular |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
793 |
expressions and want to use them for language processing you |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
794 |
often want to recognise keywords in a language, for example |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
795 |
\code{for},{} \code{if},{} \code{yield} and so on. But the |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
796 |
basic regular expression \code{CHAR} can only recognise a |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
797 |
single character. In order to recognise a whole string, like |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
798 |
\code{for}, you have to put many of those together using |
235
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
799 |
\code{SEQ}: |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
800 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
801 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
802 |
\begin{lstlisting}[numbers=none] |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
803 |
SEQ(CHAR('f'), SEQ(CHAR('o'), CHAR('r'))) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
804 |
\end{lstlisting} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
805 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
806 |
\noindent This gets quickly unreadable when the strings and |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
807 |
regular expressions get more complicated. In other functional |
247
84b4bc6e8554
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
245
diff
changeset
|
808 |
programming languages, you can explicitly write a conversion |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
809 |
function that takes a string, say \dq{\pcode{for}}, and |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
810 |
generates the regular expression above. But then your code is |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
811 |
littered with such conversion functions. |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
812 |
|
235
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
813 |
In Scala you can do better by ``hiding'' the conversion |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
814 |
functions. The keyword for doing this is \code{implicit} and |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
815 |
it needs a built-in library called |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
816 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
817 |
\begin{lstlisting}[numbers=none] |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
818 |
scala.language.implicitConversions |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
819 |
\end{lstlisting} |
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
820 |
|
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
821 |
\noindent |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
822 |
Consider the code |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
823 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
824 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
825 |
\begin{lstlisting}[language=Scala] |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
826 |
import scala.language.implicitConversions |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
827 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
828 |
def charlist2rexp(s: List[Char]) : Rexp = s match { |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
829 |
case Nil => EMPTY |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
830 |
case c::Nil => CHAR(c) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
831 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
832 |
} |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
833 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
834 |
implicit def string2rexp(s: String) : Rexp = |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
835 |
charlist2rexp(s.toList) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
836 |
\end{lstlisting} |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
837 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
838 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
839 |
\noindent where the first seven lines implement a function |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
840 |
that given a list of characters generates the corresponding |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
841 |
regular expression. In Lines 9 and 10, this function is used |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
842 |
for transforming a string into a regular expression. Since the |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
843 |
\code{string2rexp}-function is declared as \code{implicit}, |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
844 |
the effect will be that whenever Scala expects a regular |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
845 |
expression, but I only give it a string, it will automatically |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
846 |
insert a call to the \code{string2rexp}-function. I can now |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
847 |
write for example |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
848 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
849 |
\begin{lstlisting}[numbers=none] |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
850 |
scala> ALT("ab", "ac") |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
851 |
res9 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c))) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
852 |
\end{lstlisting} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
853 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
854 |
\noindent Recall that \code{ALT} expects two regular |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
855 |
expressions as arguments, but I only supply two strings. The |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
856 |
implicit conversion function will transform the string into a |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
857 |
regular expression. |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
858 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
859 |
Using implicit definitions, Scala allows me to introduce |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
860 |
some further syntactic sugar for regular expressions: |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
861 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
862 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
863 |
\begin{lstlisting}[ numbers=none] |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
864 |
implicit def RexpOps(r: Rexp) = new { |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
865 |
def | (s: Rexp) = ALT(r, s) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
866 |
def ~ (s: Rexp) = SEQ(r, s) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
867 |
def % = STAR(r) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
868 |
} |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
869 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
870 |
implicit def stringOps(s: String) = new { |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
871 |
def | (r: Rexp) = ALT(s, r) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
872 |
def | (r: String) = ALT(s, r) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
873 |
def ~ (r: Rexp) = SEQ(s, r) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
874 |
def ~ (r: String) = SEQ(s, r) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
875 |
def % = STAR(s) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
876 |
} |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
877 |
\end{lstlisting} |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
878 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
879 |
|
232
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
880 |
\noindent This might seem a bit overly complicated, but its effect is |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
881 |
that I can now write regular expressions such as $ab + ac$ |
316 | 882 |
simply as |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
883 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
884 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
885 |
\begin{lstlisting}[numbers=none] |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
886 |
scala> "ab" | "ac" |
238
527fdb90fffe
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
237
diff
changeset
|
887 |
res10 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c))) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
888 |
\end{lstlisting} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
889 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
890 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
891 |
\noindent I leave you to figure out what the other |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
892 |
syntactic sugar in the code above stands for. |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
893 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
894 |
One more useful feature of Scala is the ability to define |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
895 |
functions with varying argument lists. This is a feature that |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
896 |
is already present in old languages, like C, but seems to have |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
897 |
been forgotten in the meantime---Java does not have it. In the |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
898 |
context of regular expressions this feature comes in handy: |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
899 |
Say you are fed up with writing many alternatives as |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
900 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
901 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
902 |
\begin{lstlisting}[numbers=none] |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
903 |
ALT(..., ALT(..., ALT(..., ...))) |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
904 |
\end{lstlisting} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
905 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
906 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
907 |
\noindent To make it difficult, you do not know how deep such |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
908 |
alternatives are nested. So you need something flexible that |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
909 |
can take as many alternatives as needed. In Scala one can |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
910 |
achieve this by adding a \code{*} to the type of an argument. |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
911 |
Consider the code |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
912 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
913 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
914 |
\begin{lstlisting}[language=Scala] |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
915 |
def Alts(rs: List[Rexp]) : Rexp = rs match { |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
916 |
case Nil => NULL |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
917 |
case r::Nil => r |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
918 |
case r::rs => ALT(r, Alts(rs)) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
919 |
} |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
920 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
921 |
def ALTS(rs: Rexp*) = Alts(rs.toList) |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
922 |
\end{lstlisting} |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
923 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
924 |
|
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
925 |
\noindent The function in Lines 1 to 5 takes a list of regular |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
926 |
expressions and converts it into an appropriate alternative |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
927 |
regular expression. In Line 7 there is a wrapper for this |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
928 |
function which uses the feature of varying argument lists. The |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
929 |
effect of this code is that I can write the regular |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
930 |
expression for keywords as |
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
931 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
932 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
933 |
\begin{lstlisting}[numbers=none] |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
934 |
ALTS("for", "def", "yield", "implicit", "if", "match", "case") |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
935 |
\end{lstlisting} |
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
936 |
|
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
937 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
938 |
\noindent Again I leave it to you to find out how much this |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
939 |
simplifies the regular expression in comparison with if I had |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
940 |
to write this by hand using only the ``plain'' regular |
233
acddd4808117
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
941 |
expressions from the inductive datatype. |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
942 |
|
229
00c4fda3d6c5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
228
diff
changeset
|
943 |
\subsection*{More Info} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
944 |
|
232
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
945 |
There is much more to Scala than I can possibly describe in |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
946 |
this document. Fortunately there are a number of free books |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
947 |
about Scala and of course lots of help online. For example |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
948 |
|
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
949 |
\begin{itemize} |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
950 |
\item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf} |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
951 |
\item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf} |
233
acddd4808117
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
232
diff
changeset
|
952 |
\item \url{https://www.youtube.com/user/ShadowofCatron} |
397
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
316
diff
changeset
|
953 |
\item \url{http://docs.scala-lang.org/tutorials} |
405
30dd644ba71a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
402
diff
changeset
|
954 |
\item \url{https://www.scala-exercises.org} |
232
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
955 |
\end{itemize} |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
956 |
|
237
370c0647a9bf
more material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
957 |
\noindent There is also a course at Coursera on Functional |
370c0647a9bf
more material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
958 |
Programming Principles in Scala by Martin Odersky, the main |
397
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
316
diff
changeset
|
959 |
developer of the Scala language. And a document that explains |
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
316
diff
changeset
|
960 |
Scala for Java programmers |
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
316
diff
changeset
|
961 |
|
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
316
diff
changeset
|
962 |
\begin{itemize} |
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
316
diff
changeset
|
963 |
\item \small\url{http://docs.scala-lang.org/tutorials/scala-for-java-programmers.html} |
cf3ca219c727
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
316
diff
changeset
|
964 |
\end{itemize} |
237
370c0647a9bf
more material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
965 |
|
232
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
966 |
While I am quite enthusiastic about Scala, I am also happy to |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
967 |
admit that it has more than its fair share of faults. The |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
968 |
problem seen earlier of having to give an explicit type to |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
969 |
\code{toSet}, but not \code{toList} is one of them. There are |
232
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
970 |
also many ``deep'' ideas about types in Scala, which even to |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
971 |
me as seasoned functional programmer are puzzling. Whilst |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
972 |
implicits are great, they can also be a source of great |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
973 |
headaches, for example consider the code: |
231
47bcc2178f4e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
974 |
|
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
975 |
\begin{lstlisting}[numbers=none] |
231
47bcc2178f4e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
976 |
scala> List (1, 2, 3) contains "your mom" |
47bcc2178f4e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
977 |
res1: Boolean = false |
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
978 |
\end{lstlisting} |
231
47bcc2178f4e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
979 |
|
234
bf7eecc9cefe
updated scala ho
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
233
diff
changeset
|
980 |
\noindent Rather than returning \code{false}, this code should |
232
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
981 |
throw a typing-error. There are also many limitations Scala |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
982 |
inherited from the JVM that can be really annoying. For |
235
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
983 |
example a fixed stack size. One can work around this |
bc460179148c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
234
diff
changeset
|
984 |
particular limitation, but why does one have to? |
231
47bcc2178f4e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
985 |
|
232
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
986 |
Even if Scala has been a success in several high-profile |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
987 |
companies, there is also a company (Yammer) that first used |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
988 |
Scala in their production code, but then moved away from it. |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
989 |
Allegedly they did not like the steep learning curve of Scala |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
990 |
and also that new versions of Scala often introduced |
240
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
991 |
incompatibilities in old code. In the past two months |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
992 |
there have also been two forks of the Scala compiler. |
de4f6382590a
polished
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
239
diff
changeset
|
993 |
It needs to be seen what the future brings for Scala. |
231
47bcc2178f4e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
230
diff
changeset
|
994 |
|
232
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
995 |
So all in all, Scala might not be a great teaching language, |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
996 |
but I hope this is mitigated by the fact that I never require |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
997 |
you to write any Scala code. You only need to be able to read |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
998 |
it. In the coursework you can use any programming language you |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
999 |
like. If you want to use Scala for this, then be my guest; if |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
1000 |
you do not want, stick with the language you are most familiar |
2c512713f08a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
231
diff
changeset
|
1001 |
with. |
230
0fd668d7b619
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
229
diff
changeset
|
1002 |
|
237
370c0647a9bf
more material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
1003 |
|
370c0647a9bf
more material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
236
diff
changeset
|
1004 |
|
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1005 |
\end{document} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1006 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1007 |
%%% Local Variables: |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1008 |
%%% mode: latex |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1009 |
%%% TeX-master: t |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1010 |
%%% End: |