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