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