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