handouts/pep-ho.tex
author Christian Urban <urbanc@in.tum.de>
Fri, 05 Oct 2018 11:23:15 +0100
changeset 192 cd2a9c969ef2
parent 188 e075b5015e89
child 193 b8e3f32d6097
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\usepackage{../style}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
\usepackage{../langs}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
\usepackage{marvosym}
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
     5
\usepackage{boxedminipage}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
%cheat sheet
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
%http://worldline.github.io/scala-cheatsheet/
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    10
% case class, apply, unapply
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    11
% see https://medium.com/@thejasbabu/scala-pattern-matching-9c9e73ba9a8a
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    12
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
\begin{document}
180
Christian Urban <urbanc@in.tum.de>
parents: 179
diff changeset
    14
\fnote{\copyright{} Christian Urban, King's College London, 2017, 2018}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
125
b91ad3761732 updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    16
\section*{A Crash-Course in Scala}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    18
\mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled 
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    19
\underline{a}cademic \underline{la}nguage''}\smallskip\\
192
cd2a9c969ef2 updated
Christian Urban <urbanc@in.tum.de>
parents: 188
diff changeset
    20
\mbox{}\hfill\textit{ --- a joke(?) found on Twitter}\bigskip
178
55367d499635 updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
    21
 
55367d499635 updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
    22
\noindent
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    23
Scala is a programming language that combines functional and
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    24
object-oriented programming-styles. It has received quite a bit of
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    25
attention in the last five or so years. One reason for this attention is
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    26
that, like the Java programming language, Scala compiles to the Java
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    27
Virtual Machine (JVM) and therefore Scala programs can run under MacOSX,
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    28
Linux and Windows.\footnote{There are also experimental backends of
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    29
Scala for producing code under Android (\url{http://scala-android.org});
183
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
    30
for generating JavaScript code to build browser applications
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
    31
\url{(https://www.scala-js.org)}; and there is work under way to
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    32
have a native Scala compiler generating X86-code
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    33
(\url{http://www.scala-native.org}).} Because of this it has also access to
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    34
the myriads of Java libraries. Unlike Java, however, Scala often allows
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
    35
programmers to write very concise and elegant code.  Some therefore say
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    36
``Scala is the better Java''.\footnote{from
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
    37
\url{https://www.slideshare.net/maximnovak/joy-of-scala}} 
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
    38
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
    39
A number of companies---the Guardian, Twitter, Coursera, FourSquare, Netflix,
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
    40
LinkedIn to name a few---either use Scala exclusively in
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
    41
production code, or at least to some substantial degree. Scala seems
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
    42
also useful in job-interviews (especially in data science) according to
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
    43
this anecdotal report
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    44
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    45
\begin{quote}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    46
\url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child}
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    47
\end{quote}
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    48
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    49
\noindent
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    50
The official Scala compiler can be downloaded from
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    51
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    52
\begin{quote}
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    53
\url{http://www.scala-lang.org}
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    54
\end{quote}
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    55
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    56
\noindent
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
    57
I found a convenient IDE for writing Scala programs is Microsoft's
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    58
\textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    59
obviously Windows. It can be downloaded for free from
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    60
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    61
\begin{quote}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    62
\url{https://code.visualstudio.com}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    63
\end{quote}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    64
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    65
\noindent
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    66
and should already come pre-installed in the Department (together with
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    67
the Scala compiler). VS Code is far from perfect, however it includes a
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    68
\textit{Marketplace} from which a multitude of extensions can be
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
    69
downloaded that make editing and running Scala code a little easier (see
19b1e01054f6 updated
pdated
parents: 183
diff changeset
    70
Figure~\ref{vscode} for my setup).
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    71
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    72
\begin{figure}[t]
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
    73
\begin{boxedminipage}{\textwidth}  
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    74
\begin{center}  
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    75
\includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    76
\end{center}
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    77
\caption{My personal installation of VS Code includes the following
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    78
packages from Marketplace: Scala Syntax (official), Code Runner, Code
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
    79
Spell Checker, Rewrap and Subtle Match Brackets. I have also bound 
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
    80
the keys \keys{Ctrl} \keys{Ret} to the action
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    81
``Run-Selected-Text-In-Active-Terminal'' in order to quickly evaluate
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    82
small code snippets in the Scala REPL.\label{vscode}}
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
    83
\end{boxedminipage}
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    84
\end{figure}  
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    85
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
    86
What I like most about VS Code is that it provides easy access to the
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
    87
Scala REPL. But if you prefer another editor for coding, it is also
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
    88
painless to work with Scala completely on the command line (as you might
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
    89
have done with \texttt{g++} in the earlier part of PEP). For the
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    90
lazybones among us, there is even an online editor and environment for
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    91
developing and running Scala programs called \textit{ScalaFiddle}, which
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    92
requires zero setup (assuming you have a browser handy). You can access
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
    93
it from: 
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    94
 
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    95
\begin{quote}
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
    96
\url{https://scalafiddle.io}\medskip
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    97
\end{quote}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    98
  
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    99
183
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   100
Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ.
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   101
A ready-made Scala bundle for Eclipse is available from
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   102
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   103
\begin{quote}
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   104
\url{http://scala-ide.org/download/sdk.html}
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   105
\end{quote}
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   106
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   107
\noindent
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   108
Also IntelliJ includes plugins for Scala. \textbf{BUT}, I do \textbf{not}
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   109
recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   110
seem to make your life harder, rather than easier, for the small
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   111
programs we will write in this module. They are really meant to be used
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   112
when you have a million-lines codebase, rather than our
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   113
``toy-programs''\ldots{}for example why on earth am I required to create a
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   114
completely new project with several subdirectories when I just want to
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   115
try out 20-lines of Scala code? Your mileage may vary though. ;o)
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   116
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   117
\subsection*{Why Functional Programming?}
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   118
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   119
Before we go on, let me explain a bit more why we want to inflict upon
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   120
you another programming language. You hopefully have mastered Java and
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   121
C++\ldots{}the world should be your oyster, no? Well, it is not that
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   122
easy. We do require Scala in PEP, but actually we do not religiously
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   123
care whether you learn Scala---after all it is just a programming
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   124
language (albeit a nifty one IMHO). What we do care about is that you
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   125
learn about \textit{functional programming}. Scala is just the vehicle
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   126
for that. Still, you need to learn Scala well enough to get good marks
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   127
in PEP, but functional programming could equally be taught with Haskell,
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   128
F\#, SML, Ocaml, Kotlin, Clojure, Scheme, Elm and many other functional
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   129
programming languages. %Your friendly lecturer just happens to like Scala
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   130
%and the Department agreed that it is a good idea to inflict Scala upon
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   131
%you.
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   132
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   133
Very likely writing programs in a functional programming language is
183
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   134
quite different from what you are  used to in your study so far. It
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   135
might even be totally alien to you. The reason is that functional
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   136
programming seems to go against the core principles of
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   137
\textit{imperative programming} (which is what you do in Java and C++
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   138
for example). The main idea of imperative programming  is that you have
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   139
some form of ``state'' in your program and you continuously change this
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   140
state by issuing some commands (e.g.~updating a field in an array or
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   141
object, adding one to a variable and so on). The classic example for
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   142
this style of programming are \texttt{for}-loops, for example
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   143
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   144
\begin{lstlisting}[language=C,numbers=none]
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   145
for (int i = 10; i < 20; i++) { 
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   146
      //...Do something interesting with i...
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   147
}
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   148
\end{lstlisting}
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   149
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   150
\noindent Here the integer variable \texttt{i} embodies the state, which
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   151
is first set to \texttt{10} and then increased by one in each
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   152
loop-iteration until it reaches \texttt{20} at which point the loop
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   153
exits. When this code is compiled and actually runs, there will be some
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   154
dedicated space reserved for \texttt{i} in memory. This space of
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   155
typically 32 bits contains \texttt{i}'s current value\ldots\texttt{10}
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   156
at the beginning, and then the content will be updated by, or
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   157
overwritten with, some new content in every iteration. The main point
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   158
here is that this kind of updating, or manipulating, memory is
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   159
25.806\ldots or \textbf{THE ROOT OF ALL EVIL}!!
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   160
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   161
\begin{center}
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   162
\includegraphics[scale=0.25]{../pics/root-of-all-evil.png}
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   163
\end{center}  
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   164
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   165
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   166
\noindent
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   167
\ldots{}Well, it is perfectly benign if you have a sequential program
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   168
that gets run instruction by instruction...nicely one after another.
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   169
This kind of running code uses a single core of your CPU and goes as
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   170
fast as your CPU frequency, also called clock-speed, allows. The problem
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   171
is that this clock-speed has not much increased over the past decade and
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   172
no dramatic increases are predicted for any time soon. So you are a bit
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   173
stuck, unlike previous generations of developers who could rely upon the
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   174
fact that every 2 years or so their code would run twice as fast (in
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   175
ideal circumstances) because the clock-speed of their CPUs got twice as
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   176
fast. This unfortunately does not happen any more nowadays. To get you
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   177
out of this dreadful situation, CPU producers pile more and more
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   178
cores into CPUs in order to make them more powerful and potentially make
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   179
software faster. The task for you as developer is to take somehow
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   180
advantage of these cores by running as much of your code as possible in
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   181
parallel on as many cores you have available (typically 4 in modern
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   182
laptops and sometimes much more on high-end machines). In this
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   183
situation, \textit{mutable} variables like \texttt{i} above are evil, or
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   184
at least a major nuisance: Because if you want to distribute some of the
183
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   185
loop-iterations over the cores that are currently idle in your system,
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   186
you need to be extremely careful about who can read and overwrite
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   187
the variable \texttt{i}.\footnote{If you are of the mistaken belief that nothing
183
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   188
nasty can happen to \texttt{i} inside the \texttt{for}-loop, then you
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   189
need to go back over the C++ material.} Especially the writing operation
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   190
is critical because you do not want that conflicting writes mess about
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   191
with \texttt{i}. Take my word: an untold amount of misery has arisen
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   192
from this problem. The catch is that if you try to solve this problem in
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   193
C++ or Java, and be as defensive as possible about reads and writes to
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   194
\texttt{i}, then you need to synchronise access to it. The result is that
183
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   195
your program more often than not waits more than it runs, thereby
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   196
defeating the point of trying to run the program in parallel in the
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   197
first place. If you are less defensive, then usually all hell breaks
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   198
loose by seemingly obtaining random results. And forget the idea of
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   199
being able to debug such code.
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   200
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   201
The central idea of functional programming is to eliminate any state
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   202
from programs---or at least from the ``interesting bits''. Because then
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   203
it is easy to parallelize the resulting programs: if you do not have any
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   204
state, then once created, all memory content stays unchanged and reads
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   205
to such memory are absolutely safe without the need of any
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   206
synchronisation. An example is given in Figure~\ref{mand} where in the
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   207
absence of the annoying state, Scala makes it very easy to calculate the
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   208
Mandelbrot set on as many cores of your CPU as possible. Why is it so
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   209
easy in this example? Because each pixel in the Mandelbrot set can be
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   210
calculated independently and the calculation does not need to update any
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   211
variable. It is so easy in fact that going from the sequential version
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   212
of the Mandelbrot program to the parallel version can be achieved by
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   213
adding just eight characters---in two places you have to add
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   214
\texttt{.par}. Try the same in C++ or Java!
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   215
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   216
\begin{figure}[p]
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   217
\begin{boxedminipage}{\textwidth}
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   218
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   219
A Scala program for generating pretty pictures of the Mandelbrot set 
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   220
(\url{https://en.wikipedia.org/wiki/Mandelbrot_set},
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   221
\url{https://www.youtube.com/watch?v=aSg2Db3jF_4}):
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   222
\begin{center}    
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   223
\begin{tabular}{c}  
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   224
\includegraphics[scale=0.12]{../pics/mand1.png}
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   225
\end{tabular}
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   226
\end{center}
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   227
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   228
\begin{center}
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   229
\begin{tabular}{@{}p{0.45\textwidth}|p{0.45\textwidth}@{}}
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   230
  \bf sequential version: & \bf parallel version (on 4 cores):\smallskip\\
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   231
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   232
  {\hfill\includegraphics[scale=0.12]{../pics/mand4.png}\hfill} &
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   233
  {\hfill\includegraphics[scale=0.12]{../pics/mand3.png}\hfill} \\
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   234
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   235
{\footnotesize\begin{lstlisting}[xleftmargin=-1mm]
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   236
for (y <- (0 until H)) {
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   237
  for (x <- (0 until W)) {
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   238
    
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   239
    val c = start + 
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   240
      (x * d_x + y * d_y * i)
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   241
    val iters = iterations(c, max) 
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   242
    val col = 
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   243
      if (iters == max) black 
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   244
      else colours(iters % 16)
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   245
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   246
    pixel(x, y, col)
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   247
  }
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   248
  viewer.updateUI()
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   249
}   
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   250
\end{lstlisting}}   
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   251
& 
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   252
{\footnotesize\begin{lstlisting}[xleftmargin=0mm]
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   253
for (y <- (0 until H)/*@\keys{\texttt{.par}}@*/) {
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   254
  for (x <- (0 until W)/*@\keys{\texttt{.par}}@*/) {
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   255
      
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   256
    val c = start + 
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   257
      (x * d_x + y * d_y * i)
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   258
    val iters = iterations(c, max) 
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   259
    val col = 
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   260
      if (iters == max) black 
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   261
      else colours(iters % 16)
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   262
  
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   263
    pixel(x, y, col)
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   264
  }
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   265
  viewer.updateUI()
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   266
}   
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   267
\end{lstlisting}}\\
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   268
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   269
\centering\includegraphics[scale=0.5]{../pics/cpu2.png} &
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   270
\centering\includegraphics[scale=0.5]{../pics/cpu1.png}
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   271
\end{tabular}
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   272
\end{center}
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   273
\caption{The main ``loops'' creating the picture of the Mandelbrot set. The parallel version
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   274
only differs in \texttt{.par} added to the ``ranges'' of the x-y-coordinates. As can be
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   275
seen from the CPU loads: in the sequential versions there is a lower peak for an
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   276
extended period, while in the parallel version there is a short sharp burst for 
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   277
essentially the same workload.
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   278
\label{mand}} 
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   279
\end{boxedminipage}
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   280
\end{figure}  
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   281
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   282
But remember this easy parallelisation of code requires that we
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   283
have no state in our programs\ldots{}that is no counters like
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   284
\texttt{i} in \texttt{for}-loops. You might then ask, how do I write
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   285
loops without such counters? Well, teaching you that this is possible is
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   286
one of the main points of the Scala-part in PEP. I can assure you it is
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   287
possible, but you have to get your head around it. Once you have
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   288
mastered this, it will be fun to have no state in your programs (a side
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   289
product is that it much easier to debug state-less code and also more
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   290
often than not easier to understand). So have fun with
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   291
Scala!\footnote{If you are still not convinced about the function
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   292
programming ``thing'', there are a few more arguments: a lot of research
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   293
in programming languages happens to take place in functional programming
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   294
languages. This has resulted in ultra-useful features such as
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   295
pattern-matching, strong type-systems, lazyness, implicits, algebraic
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   296
datatypes  to name a few. Imperative languages seem to often lag behind
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   297
in adopting them: I know, for example, that Java will at some point in
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   298
the future support pattern-matching, which has been used in SML for at
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   299
least 40(!) years. See
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   300
\url{http://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html}.
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   301
Also Rust, a C-like programming language that has been developed since
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   302
2010 and is gaining quite some interest, borrows many ideas from
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   303
functional programming from yesteryear.}
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   304
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   305
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
\subsection*{The Very Basics}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
One advantage of Scala over Java is that it includes an interpreter (a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
REPL, or
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
\underline{R}ead-\underline{E}val-\underline{P}rint-\underline{L}oop)
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   311
with which you can run and test small code snippets without the need
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
of a compiler. This helps a lot with interactively developing
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   313
programs. It is my preferred way of writing small Scala
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
programs. Once you installed Scala, you can start the interpreter by
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   315
typing on the command line:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
$ scala
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   319
Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit Server VM, Java 9).
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
Type in expressions for evaluation. Or try :help.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
scala>
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
\end{lstlisting}%$
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
\noindent The precise response may vary depending
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
on the version and platform where you installed Scala. At the Scala
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
prompt you can type things like \code{2 + 3}\;\keys{Ret} and
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
the output will be
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
scala> 2 + 3
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
res0: Int = 5
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   335
\noindent The answer means that he result of the addition is of type
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   336
\code{Int} and the actual result is 5; \code{res0} is a name that
125
b91ad3761732 updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   337
Scala gives automatically to the result. You can reuse this name later
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   338
on, for example
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   339
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   340
\begin{lstlisting}[numbers=none]
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   341
scala> res0 + 4
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   342
res1: Int = 9
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   343
\end{lstlisting}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   344
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   345
\noindent
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   346
Another classic example you can try out is
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   347
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   349
scala> print("hello world")
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
hello world
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
\noindent Note that in this case there is no result. The
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
reason is that \code{print} does not actually produce a result
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   355
(there is no \code{resX} and no type), rather it is a
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
function that causes the \emph{side-effect} of printing out a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
string. Once you are more familiar with the functional
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
programming-style, you will know what the difference is
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
between a function that returns a result, like addition, and a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
function that causes a side-effect, like \code{print}. We
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
shall come back to this point later, but if you are curious
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
now, the latter kind of functions always has \code{Unit} as
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   363
return type. It is just not printed by Scala. 
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   365
You can try more examples with the Scala REPL, but feel free to
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   366
first guess what the result is (not all answers by Scala are obvious):
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
scala> 2 + 2
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
scala> 1 / 2
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
scala> 1.0 / 2
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
scala> 1 / 2.0
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
scala> 1 / 0
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
scala> 1.0 / 0.0
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
scala> true == false
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   376
scala> true && false
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   377
scala> 1 > 1.0
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
scala> "12345".length
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   379
scala> List(1,2,1).size
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   380
scala> Set(1,2,1).size
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   381
\end{lstlisting}\smallskip
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   383
\noindent
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   384
Please take the Scala REPL seriously: If you want to take advantage of my
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   385
reference implementation for the assignments, you will need to be
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   386
able to ``play around'' with it!
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   387
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   388
\subsection*{Standalone Scala Apps}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
If you want to write a stand-alone app in Scala, you can
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   391
implement an object that is an instance of \code{App}, say
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   392
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
object Hello extends App {
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
    println("hello world")
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   396
}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   397
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   398
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   399
\noindent save it in a file, for example {\tt hello-world.scala}, and
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   400
then run the compiler (\texttt{scalac}) and start the runtime
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   401
environment (\texttt{scala}):
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   402
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   403
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   404
$ scalac hello-world.scala
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   405
$ scala Hello
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   406
hello world
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   407
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   408
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   409
\noindent
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   410
Like Java, Scala targets the JVM and consequently
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   411
Scala programs can also be executed by the bog-standard Java
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   412
Runtime. This only requires the inclusion of {\tt
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   413
scala-library.jar}, which on my computer can be done as
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   414
follows:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   416
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   417
$ scalac hello-world.scala
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   418
$ java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
hello world
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   420
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   421
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   422
\noindent You might need to adapt the path to where you have
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   423
installed Scala.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   424
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   425
\subsection*{Values}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   426
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   427
In the lectures I will try to avoid as much as possible the term
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   428
\emph{variables} familiar from other programming languages. The reason
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   429
is that Scala has \emph{values}, which can be seen as abbreviations of
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   430
larger expressions. For example
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   431
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   432
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   433
scala> val x = 42
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   434
x: Int = 42
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   435
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   436
scala> val y = 3 + 4
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   437
y: Int = 7
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   438
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   439
scala> val z = x / y
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   440
z: Int = 6
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   441
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   442
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   443
\noindent
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   444
Why the kerfuffle about values? Well, values are \emph{immutable}. You 
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   445
cannot change their value after you defined them. If you try to reassign
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   446
\code{z} above, Scala will yell at you:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   449
scala> z = 9
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   450
error: reassignment to val
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
       z = 9
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   452
         ^
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   454
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   455
\noindent
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   456
So it would be a bit absurd to call values as variables...you cannot
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   457
change them; they cannot vary. You might think you can re-assign them like
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   458
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   459
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   460
scala> val x = 42
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   461
scala> val z = x / 7
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   462
scala> val x = 70
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   463
scala> println(z) 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   464
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   465
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   466
\noindent but try to guess what Scala will print out 
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   467
for \code{z}?  Will it be \code{6} or \code{10}? A final word about
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
values: Try to stick to the convention that names of values should be
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   469
lower case, like \code{x}, \code{y}, \code{foo41} and so on. Upper-case
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   470
names you should reserve for what is called \emph{constructors}.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   471
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   473
\subsection*{Function Definitions}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   474
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   475
We do functional programming! So defining functions will be our main occupation.
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   476
As an example, a function named \code{f} taking a single argument of type 
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   477
\code{Int} can be defined in Scala as follows:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   479
\begin{lstlisting}[numbers=none]
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   480
def f(x: Int) : String = ...EXPR...
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   481
\end{lstlisting} 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   482
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   483
\noindent
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   484
This function returns the value resulting from evaluating the expression
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   485
\code{EXPR} (whatever is substituted for this). The result will be
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   486
of type \code{String}. It is a good habit to include this information
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   487
about the return type always. Simple examples of Scala functions are:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   488
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   489
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   490
def incr(x: Int) : Int = x + 1
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   491
def double(x: Int) : Int = x + x
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   492
def square(x: Int) : Int = x * x
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   493
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   495
\noindent
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   496
The general scheme for a function is
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   497
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   500
  BODY
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
\noindent
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   505
where each argument requires its type and the result type of the
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   506
function, \code{rty}, should be given. If the body of the function is
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   507
more complex, then it can be enclosed in braces, like above. If it it
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   508
is just a simple expression, like \code{x + 1}, you can omit the
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   509
braces. Very often functions are recursive (call themselves) like
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   510
the venerable factorial function:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   512
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   513
def fact(n: Int): Int = 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   514
  if (n == 0) 1 else n * fact(n - 1)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   515
\end{lstlisting}
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   516
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   517
\noindent
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   518
Note that Scala does not have a \code{then}-keyword in an \code{if}-statement.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
  
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   520
\subsection*{Loops, or better the Absence thereof}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   522
Coming from Java or C++, you might be surprised that Scala does
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   523
not really have loops. It has instead, what is in functional
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   524
programming called, \emph{maps}. To illustrate how they work,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
let us assume you have a list of numbers from 1 to 8 and want to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   526
build the list of squares. The list of numbers from 1 to 8 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
can be constructed in Scala as follows:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
scala> (1 to 8).toList
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   531
res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
\noindent Generating from this list, the list of squares in a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
programming language such as Java, you would assume the list
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
is given as a kind of array. You would then iterate, or loop,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
an index over this array and replace each entry in the array
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
by the square. Right? In Scala, and in other functional
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
programming languages, you use maps to achieve the same. 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
A map essentially takes a function that describes how each
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
element is transformed (for example squared) and a list over
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   543
which this function should work. There are two forms to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   544
express such maps in Scala. The first way is called a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
\emph{for-comprehension}. Squaring the numbers from 1 to 8
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
would look as follows:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
scala> for (n <- (1 to 8).toList) yield n * n
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
res2: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
\noindent The important keywords are \code{for} and
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
\code{yield}. This for-comprehension roughly states that from
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   555
the list of numbers we draw \code{n}s and compute the result
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   556
of \code{n * n}. As you can see, we specified the list where
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   557
each \code{n} comes from, namely \code{(1 to 8).toList}, and
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
how each element needs to be transformed. This can also be
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
expressed in a second way in Scala by using directly
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
\code{map}s as follows:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   561
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
scala> (1 to 8).toList.map(n => n * n)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
res3 = List(1, 4, 9, 16, 25, 36, 49, 64)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
\noindent In this way, the expression \code{n => n * n} stands
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
for the function that calculates the square (this is how the
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   569
\code{n}s are transformed). This expression for functions
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
might remind you of your lessons about the lambda-calculus
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
where this would have been written as $\lambda n.\,n * n$. It
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
might not be obvious, but for-comprehensions are just
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
syntactic sugar: when compiling, Scala translates
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
for-comprehensions into equivalent maps. This even works
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
when for-comprehensions get more complicated (see below).
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
The very charming feature of Scala is that such maps or
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
for-comprehensions can be written for any kind of data
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
collection, such as lists, sets, vectors, options and so on.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
For example if we instead compute the reminders modulo 3 of
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
this list, we can write
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   583
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   584
scala> (1 to 8).toList.map(n => n % 3)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
res4 = List(1, 2, 0, 1, 2, 0, 1, 2)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   586
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
\noindent If we, however, transform the numbers 1 to 8 not
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
into a list, but into a set, and then compute the reminders
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
modulo 3 we obtain
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   593
scala> (1 to 8).toSet[Int].map(n => n % 3)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
res5 = Set(2, 1, 0)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   595
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   596
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
\noindent This is the correct result for sets, as there are
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   598
only three equivalence classes of integers modulo 3. Note that
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   599
in this example we need to ``help'' Scala to transform the
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   600
numbers into a set of integers by explicitly annotating the
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   601
type \code{Int}. Since maps and for-comprehensions are
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
just syntactic variants of each other, the latter can also be
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
written as
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   605
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   606
scala> for (n <- (1 to 8).toSet[Int]) yield n % 3
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
res5 = Set(2, 1, 0)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   608
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   609
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
For-comprehensions can also be nested and the selection of 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   611
elements can be guarded. For example if we want to pair up
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   612
the numbers 1 to 4 with the letters a to c, we can write
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   613
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   614
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
scala> for (n <- (1 to 4).toList; 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   616
            m <- ('a' to 'c').toList) yield (n, m)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   617
res6 = List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   618
            (3,a), (3,b), (3,c), (4,a), (4,b), (4,c))
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   619
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   620
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   621
\noindent 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   622
Or if we want to find all pairs of numbers between 1 and 3
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   623
where the sum is an even number, we can write
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   624
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   625
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   626
scala> for (n <- (1 to 3).toList; 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   627
            m <- (1 to 3).toList;
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   628
            if (n + m) % 2 == 0) yield (n, m)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   629
res7 = List((1,1), (1,3), (2,2), (3,1), (3,3))
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   630
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   631
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   632
\noindent The \code{if}-condition in the for-comprehension
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   633
filters out all pairs where the sum is not even.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   634
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   635
While hopefully this all looks reasonable, there is one
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   636
complication: In the examples above we always wanted to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   637
transform one list into another list (e.g.~list of squares),
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   638
or one set into another set (set of numbers into set of
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   639
reminders modulo 3). What happens if we just want to print out
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   640
a list of integers? Then actually the for-comprehension
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   641
needs to be modified. The reason is that \code{print}, you
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   642
guessed it, does not produce any result, but only produces
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   643
what is in the functional-programming-lingo called a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   644
side-effect. Printing out the list of numbers from 1 to 5
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   645
would look as follows
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   646
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   647
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   648
scala> for (n <- (1 to 5).toList) print(n)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   649
12345
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   650
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   651
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   652
\noindent
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   653
where you need to omit the keyword \code{yield}. You can
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   654
also do more elaborate calculations such as
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   655
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   656
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   657
scala> for (n <- (1 to 5).toList) {
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   658
  val square_n = n * n
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   659
  println(s"$n * $n = $square_n") 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   660
}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   661
1 * 1 = 1
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   662
2 * 2 = 4
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   663
3 * 3 = 9
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   664
4 * 4 = 16
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   665
5 * 5 = 25
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   666
\end{lstlisting}%$
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   667
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   668
\noindent In this code I use a variable assignment (\code{val
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   669
square_n = ...} ) and also what is called in Scala a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   670
\emph{string interpolation}, written \code{s"..."}. The latter
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   671
is for printing out an equation. It allows me to refer to the
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   672
integer values \code{n} and \code{square\_n} inside a string.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   673
This is very convenient for printing out ``things''. 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   674
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   675
The corresponding map construction for functions with 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   676
side-effects is in Scala called \code{foreach}. So you 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   677
could also write
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   678
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   679
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   680
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   681
scala> (1 to 5).toList.foreach(n => print(n))
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   682
12345
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   683
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   684
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   685
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   686
\noindent or even just
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   687
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   688
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   689
scala> (1 to 5).toList.foreach(print)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   690
12345
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   691
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   692
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   693
\noindent Again I hope this reminds you a bit of your
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   694
lambda-calculus lessons, where an explanation is given why
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   695
both forms produce the same result.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   696
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   697
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   698
If you want to find out more about maps and functions with
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   699
side-effects, you can ponder about the response Scala gives if
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   700
you replace \code{foreach} by \code{map} in the expression
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   701
above. Scala will still allow \code{map} with side-effect
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   702
functions, but then reacts with a slightly interesting result.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   703
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   704
\subsection*{Types}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   705
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   706
In most functional programming languages, types play an
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   707
important role. Scala is such a language. You have already
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   708
seen built-in types, like \code{Int}, \code{Boolean},
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   709
\code{String} and \code{BigInt}, but also user-defined ones,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   710
like \code{Rexp}. Unfortunately, types can be a thorny
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   711
subject, especially in Scala. For example, why do we need to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   712
give the type to \code{toSet[Int]}, but not to \code{toList}?
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   713
The reason is the power of Scala, which sometimes means it
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   714
cannot infer all necessary typing information. At the
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   715
beginning while getting familiar with Scala, I recommend a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   716
``play-it-by-ear-approach'' to types. Fully understanding
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   717
type-systems, especially complicated ones like in Scala, can
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   718
take a module on their own.\footnote{Still, such a study can
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   719
be a rewarding training: If you are in the business of
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   720
designing new programming languages, you will not be able to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   721
turn a blind eye to types. They essentially help programmers
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   722
to avoid common programming errors and help with maintaining
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   723
code.}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   724
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   725
In Scala, types are needed whenever you define an inductive
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   726
datatype and also whenever you define functions (their
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   727
arguments and their results need a type). Base types are types
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   728
that do not take any (type)arguments, for example \code{Int}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   729
and \code{String}. Compound types take one or more arguments,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   730
which as seen earlier need to be given in angle-brackets, for
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   731
example \code{List[Int]} or \code{Set[List[String]]} or 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   732
\code{Map[Int, Int]}.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   733
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   734
There are a few special type-constructors that fall outside
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   735
this pattern. One is for tuples, where the type is written
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   736
with parentheses. For example 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   737
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   738
\begin{lstlisting}[ numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   739
(Int, Int, String)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   740
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   741
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   742
\noindent is for a triple (a tuple with three components---two
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   743
integers and a string). Tuples are helpful if you want to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   744
define functions with multiple results, say the function
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   745
returning the quotient and reminder of two numbers. For this
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   746
you might define:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   747
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   748
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   749
\begin{lstlisting}[ numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   750
def quo_rem(m: Int, n: Int) : (Int, Int) = (m / n, m % n)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   751
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   752
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   753
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   754
\noindent Since this function returns a pair of integers, its
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   755
return type needs to be of type \code{(Int, Int)}.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   756
Incidentally, this is also the input type of this function.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   757
Notice this function takes \emph{two} arguments, namely
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   758
\code{m} and \code{n}, both of which are integers. They are
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   759
``packaged'' in a pair. Consequently the complete type of
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   760
\code{quo_rem} is
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   761
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   762
\begin{lstlisting}[ numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   763
(Int, Int) => (Int, Int)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   764
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   765
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   766
Another special type-constructor is for functions, written as
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   767
the arrow \code{=>}. For example, the type \code{Int =>
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   768
String} is for a function that takes an integer as input
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   769
argument and produces a string as result. A function of this
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   770
type is for instance
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   771
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   772
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   773
def mk_string(n: Int) : String = n match {
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   774
  case 0 => "zero"
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   775
  case 1 => "one"
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   776
  case 2 => "two"
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   777
  case _ => "many" 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   778
} 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   779
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   780
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   781
\noindent It takes an integer as input argument and returns a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   782
string. Unlike other functional programming languages, there
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   783
is in Scala no easy way to find out the types of existing
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   784
functions, except by looking into the documentation
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   785
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   786
\begin{quote}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   787
\url{http://www.scala-lang.org/api/current/}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   788
\end{quote}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   789
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   790
The function arrow can also be iterated, as in 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   791
\code{Int => String => Boolean}. This is the type for a function
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   792
taking an integer as first argument and a string as second,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   793
and the result of the function is a boolean. Though silly, a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   794
function of this type would be
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   795
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   796
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   797
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   798
def chk_string(n: Int)(s: String) : Boolean = 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   799
  mk_string(n) == s
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   800
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   801
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   802
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   803
\noindent which checks whether the integer \code{n}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   804
corresponds to the name \code{s} given by the function
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   805
\code{mk\_string}. Notice the unusual way of specifying the
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   806
arguments of this function: the arguments are given one after
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   807
the other, instead of being in a pair (what would be the type
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   808
of this function then?). This way of specifying the arguments
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   809
can be useful, for example in situations like this
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   810
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   811
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   812
scala> List("one", "two", "three", "many").map(chk_string(2))
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   813
res4 = List(false, true, false, false)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   814
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   815
scala> List("one", "two", "three", "many").map(chk_string(3))
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   816
res5 = List(false, false, false, true)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   817
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   818
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   819
\noindent In each case we can give to \code{map} a specialised
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   820
version of \code{chk_string}---once specialised to 2 and once
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   821
to 3. This kind of ``specialising'' a function is called
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   822
\emph{partial application}---we have not yet given to this
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   823
function all arguments it needs, but only some of them.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   824
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   825
Coming back to the type \code{Int => String => Boolean}. The
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   826
rule about such function types is that the right-most type
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   827
specifies what the function returns (a boolean in this case).
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   828
The types before that specify how many arguments the function
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   829
expects and what their type is (in this case two arguments,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   830
one of type \code{Int} and another of type \code{String}).
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   831
Given this rule, what kind of function has type
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   832
\mbox{\code{(Int => String) => Boolean}}? Well, it returns a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   833
boolean. More interestingly, though, it only takes a single
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   834
argument (because of the parentheses). The single argument
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   835
happens to be another function (taking an integer as input and
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   836
returning a string). Remember that \code{mk_string} is just 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   837
such a function. So how can we use it? For this define
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   838
the somewhat silly function \code{apply_3}:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   839
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   840
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   841
def apply_3(f: Int => String): Bool = f(3) == "many"
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   842
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   843
scala> apply_3(mk_string)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   844
res6 = true
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   845
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   846
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   847
You might ask: Apart from silly functions like above, what is
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   848
the point of having functions as input arguments to other
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   849
functions? In Java there is indeed no need of this kind of
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   850
feature: at least in the past it did not allow such
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   851
constructions. I think, the point of Java 8 is to lift this
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   852
restriction. But in all functional programming languages,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   853
including Scala, it is really essential to allow functions as
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   854
input argument. Above you already seen \code{map} and
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   855
\code{foreach} which need this. Consider the functions
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   856
\code{print} and \code{println}, which both print out strings,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   857
but the latter adds a line break. You can call \code{foreach}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   858
with either of them and thus changing how, for example, five
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   859
numbers are printed.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   860
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   861
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   862
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   863
scala> (1 to 5).toList.foreach(print)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   864
12345
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   865
scala> (1 to 5).toList.foreach(println)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   866
1
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   867
2
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   868
3
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   869
4
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   870
5
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   871
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   872
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   873
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   874
\noindent This is actually one of the main design principles
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   875
in functional programming. You have generic functions like
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   876
\code{map} and \code{foreach} that can traverse data containers,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   877
like lists or sets. They then take a function to specify what
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   878
should be done with each element during the traversal. This
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   879
requires that the generic traversal functions can cope with
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   880
any kind of function (not just functions that, for example,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   881
take as input an integer and produce a string like above).
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   882
This means we cannot fix the type of the generic traversal
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   883
functions, but have to keep them
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   884
\emph{polymorphic}.\footnote{Another interesting topic about
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   885
types, but we omit it here for the sake of brevity.} 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   886
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   887
There is one more type constructor that is rather special. It
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   888
is called \code{Unit}. Recall that \code{Boolean} has two
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   889
values, namely \code{true} and \code{false}. This can be used,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   890
for example, to test something and decide whether the test
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   891
succeeds or not. In contrast the type \code{Unit} has only a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   892
single value, written \code{()}. This seems like a completely
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   893
useless type and return value for a function, but is actually
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   894
quite useful. It indicates when the function does not return
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   895
any result. The purpose of these functions is to cause
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   896
something being written on the screen or written into a file,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   897
for example. This is what is called they cause some effect on 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   898
the side, namely a new content displayed on the screen or some
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   899
new data in a file. Scala uses the \code{Unit} type to indicate
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   900
that a function does not have a result, but potentially causes
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   901
some side-effect. Typical examples are the printing functions, 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   902
like \code{print}.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   903
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   904
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   905
% \subsection*{Cool Stuff}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   906
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   907
% The first wow-moment I had with Scala was when I came across
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   908
% the following code-snippet for reading a web-page. 
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   909
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   910
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   911
% \begin{lstlisting}[ numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   912
% import io.Source
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   913
% val url = """http://www.inf.kcl.ac.uk/staff/urbanc/"""
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   914
% Source.fromURL(url)("ISO-8859-1").take(10000).mkString
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   915
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   916
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   917
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   918
% \noindent These three lines return a string containing the
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   919
% HTML-code of my webpage. It actually already does something
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   920
% more sophisticated, namely only returns the first 10000
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   921
% characters of a webpage in case it is too large. Why is that
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   922
% code-snippet of any interest? Well, try implementing
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   923
% reading-from-a-webpage in Java. I also like the possibility of
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   924
% triple-quoting strings, which I have only seen in Scala so
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   925
% far. The idea behind this is that in such a string all
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   926
% characters are interpreted literally---there are no escaped
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   927
% characters, like \verb|\n| for newlines.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   928
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   929
% My second wow-moment I had with a feature of Scala that other
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   930
% functional programming languages do not have. This feature is
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   931
% about implicit type conversions. If you have regular
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   932
% expressions and want to use them for language processing you
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   933
% often want to recognise keywords in a language, for example
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   934
% \code{for},{} \code{if},{} \code{yield} and so on. But the
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   935
% basic regular expression \code{CHAR} can only recognise a
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   936
% single character. In order to recognise a whole string, like
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   937
% \code{for}, you have to put many of those together using
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   938
% \code{SEQ}:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   939
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   940
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   941
% \begin{lstlisting}[numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   942
% SEQ(CHAR('f'), SEQ(CHAR('o'), CHAR('r')))
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   943
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   944
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   945
% \noindent This gets quickly unreadable when the strings and
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   946
% regular expressions get more complicated. In other functional
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   947
% programming languages, you can explicitly write a conversion
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   948
% function that takes a string, say \dq{\pcode{for}}, and
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   949
% generates the regular expression above. But then your code is
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   950
% littered with such conversion functions.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   951
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   952
% In Scala you can do better by ``hiding'' the conversion
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   953
% functions. The keyword for doing this is \code{implicit} and
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   954
% it needs a built-in library called 
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   955
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   956
% \begin{lstlisting}[numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   957
% scala.language.implicitConversions
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   958
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   959
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   960
% \noindent
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   961
% Consider the code
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   962
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   963
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   964
% \begin{lstlisting}[language=Scala]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   965
% import scala.language.implicitConversions
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   966
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   967
% def charlist2rexp(s: List[Char]) : Rexp = s match {
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   968
%   case Nil => EMPTY
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   969
%   case c::Nil => CHAR(c)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   970
%   case c::s => SEQ(CHAR(c), charlist2rexp(s))
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   971
% }
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   972
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   973
% implicit def string2rexp(s: String) : Rexp = 
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   974
%   charlist2rexp(s.toList)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   975
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   976
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   977
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   978
% \noindent where the first seven lines implement a function
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   979
% that given a list of characters generates the corresponding
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   980
% regular expression. In Lines 9 and 10, this function is used
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   981
% for transforming a string into a regular expression. Since the
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   982
% \code{string2rexp}-function is declared as \code{implicit},
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   983
% the effect will be that whenever Scala expects a regular
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   984
% expression, but I only give it a string, it will automatically
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   985
% insert a call to the \code{string2rexp}-function. I can now
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   986
% write for example
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   987
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   988
% \begin{lstlisting}[numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   989
% scala> ALT("ab", "ac")
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   990
% res9 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c)))
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   991
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   992
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   993
% \noindent Recall that \code{ALT} expects two regular
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   994
% expressions as arguments, but I only supply two strings. The
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   995
% implicit conversion function will transform the string into a
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   996
% regular expression.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   997
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   998
% Using implicit definitions, Scala allows me to introduce
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   999
% some further syntactic sugar for regular expressions:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1000
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1001
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1002
% \begin{lstlisting}[ numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1003
% implicit def RexpOps(r: Rexp) = new {
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1004
%   def | (s: Rexp) = ALT(r, s)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1005
%   def ~ (s: Rexp) = SEQ(r, s)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1006
%   def % = STAR(r)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1007
% }
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1008
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1009
% implicit def stringOps(s: String) = new {
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1010
%   def | (r: Rexp) = ALT(s, r)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1011
%   def | (r: String) = ALT(s, r)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1012
%   def ~ (r: Rexp) = SEQ(s, r)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1013
%   def ~ (r: String) = SEQ(s, r)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1014
%   def % = STAR(s)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1015
% }
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1016
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1017
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1018
 
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1019
% \noindent This might seem a bit overly complicated, but its effect is
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1020
% that I can now write regular expressions such as $ab + ac$ 
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1021
% simply as
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1022
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1023
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1024
% \begin{lstlisting}[numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1025
% scala> "ab" | "ac"
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1026
% res10 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c)))
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1027
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1028
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1029
 
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1030
% \noindent I leave you to figure out what the other
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1031
% syntactic sugar in the code above stands for.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1032
 
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1033
% One more useful feature of Scala is the ability to define
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1034
% functions with varying argument lists. This is a feature that
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1035
% is already present in old languages, like C, but seems to have
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1036
% been forgotten in the meantime---Java does not have it. In the
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1037
% context of regular expressions this feature comes in handy:
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1038
% Say you are fed up with writing many alternatives as
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1039
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1040
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1041
% \begin{lstlisting}[numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1042
% ALT(..., ALT(..., ALT(..., ...)))
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1043
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1044
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1045
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1046
% \noindent To make it difficult, you do not know how deep such
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1047
% alternatives are nested. So you need something flexible that
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1048
% can take as many alternatives as needed. In Scala one can
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1049
% achieve this by adding a \code{*} to the type of an argument.
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1050
% Consider the code
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1051
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1052
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1053
% \begin{lstlisting}[language=Scala]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1054
% def Alts(rs: List[Rexp]) : Rexp = rs match {
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1055
%   case Nil => NULL
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1056
%   case r::Nil => r
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1057
%   case r::rs => ALT(r, Alts(rs))
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1058
% }
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1059
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1060
% def ALTS(rs: Rexp*) = Alts(rs.toList)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1061
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1062
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1063
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1064
% \noindent The function in Lines 1 to 5 takes a list of regular
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1065
% expressions and converts it into an appropriate alternative
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1066
% regular expression. In Line 7 there is a wrapper for this
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1067
% function which uses the feature of varying argument lists. The
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1068
% effect of this code  is that I can write the regular
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1069
% expression for keywords as
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1070
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1071
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1072
% \begin{lstlisting}[numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1073
% ALTS("for", "def", "yield", "implicit", "if", "match", "case")
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1074
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1075
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1076
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1077
% \noindent Again I leave it to you to find out how much this
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1078
% simplifies the regular expression in comparison with if I had
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1079
% to write this by hand using only the ``plain'' regular
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1080
% expressions from the inductive datatype.
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1081
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1082
\bigskip\noindent
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1083
\textit{More TBD.}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1084
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
  1085
\subsection*{Coursework}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
  1086
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1087
\subsection*{More Info}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1088
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1089
There is much more to Scala than I can possibly describe in
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1090
this document. Fortunately there are a number of free books
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1091
about Scala and of course lots of help online. For example
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1092
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1093
\begin{itemize}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1094
\item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1095
\item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1096
\item \url{https://www.youtube.com/user/ShadowofCatron}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1097
\item \url{http://docs.scala-lang.org/tutorials}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1098
\item \url{https://www.scala-exercises.org}
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
  1099
\item \url{https://twitter.github.io/scala_school}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1100
\end{itemize}
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
  1101
 
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1102
\noindent There is also a course at Coursera on Functional
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1103
Programming Principles in Scala by Martin Odersky, the main
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1104
developer of the Scala language. And a document that explains
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1105
Scala for Java programmers
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1106
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1107
\begin{itemize}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1108
\item \small\url{http://docs.scala-lang.org/tutorials/scala-for-java-programmers.html}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1109
\end{itemize}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1110
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1111
While I am quite enthusiastic about Scala, I am also happy to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1112
admit that it has more than its fair share of faults. The
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1113
problem seen earlier of having to give an explicit type to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1114
\code{toSet}, but not \code{toList} is one of them. There are
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1115
also many ``deep'' ideas about types in Scala, which even to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1116
me as seasoned functional programmer are puzzling. Whilst
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1117
implicits are great, they can also be a source of great
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1118
headaches, for example consider the code:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1119
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1120
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1121
scala>  List (1, 2, 3) contains "your mom"
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1122
res1: Boolean = false
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1123
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1124
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1125
\noindent Rather than returning \code{false}, this code should
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1126
throw a typing-error. There are also many limitations Scala
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1127
inherited from the JVM that can be really annoying. For
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1128
example a fixed stack size. One can work around this
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1129
particular limitation, but why does one have to?
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1130
More such `puzzles' can be found at
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1131
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1132
\begin{center}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1133
  \url{http://scalapuzzlers.com} and
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1134
  \url{http://latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1135
\end{center}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1136
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1137
Even if Scala has been a success in several high-profile
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1138
companies, there is also a company (Yammer) that first used
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1139
Scala in their production code, but then moved away from it.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1140
Allegedly they did not like the steep learning curve of Scala
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1141
and also that new versions of Scala often introduced
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1142
incompatibilities in old code. In the past two months
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1143
there have also been two forks of the Scala compiler.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1144
It needs to be seen what the future brings for Scala.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1145
152
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1146
%So all in all, Scala might not be a great teaching language,
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1147
%but I hope this is mitigated by the fact that I never require
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1148
%you to write any Scala code. You only need to be able to read
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1149
%it. In the coursework you can use any programming language you
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1150
%like. If you want to use Scala for this, then be my guest; if
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1151
%you do not want, stick with the language you are most familiar
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1152
%with.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1153
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1154
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1155
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  1156
\begin{flushright}\it
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  1157
There are only two kinds of languages: the ones people complain 
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  1158
about\\ and the ones nobody uses.\smallskip\\
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  1159
\mbox{}\hfill\small{}---Bjarne Stroustrup (the inventor of C++)
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  1160
\end{flushright}
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  1161
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1162
\end{document}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1163
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1164
%%% Local Variables: 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1165
%%% mode: latex
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1166
%%% TeX-master: t
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1167
%%% End: