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