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