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