author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Sun, 24 Aug 2014 10:49:21 +0100 | |
changeset 228 | 4df4404455d0 |
parent 227 | 93bd75031ced |
child 229 | 00c4fda3d6c5 |
permissions | -rw-r--r-- |
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} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
\usepackage[scaled=.95]{helvet} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
\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
|
12 |
\definecolor{codegray}{gray}{0.9} |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
13 |
\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
|
14 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
\begin{document} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
\section*{A Crash-Course on Scala} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
Scala is 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
|
20 |
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
|
21 |
last five years quite a bit of attention. One reason for this |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
22 |
attention is that, like the Java programming language, it |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
23 |
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
|
24 |
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
|
25 |
experimental backends for Android and JavaScript.} Unlike |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
26 |
Java, however, Scala often allows programmers to write concise |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
27 |
and elegant code; some therefore say Scala is the much better |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
28 |
Java. If you want to try it out, the Scala compiler can be |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
downloaded from |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
\begin{quote} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
\url{http://www.scala-lang.org} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
\end{quote} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
35 |
Why do I use Scala in the AFL course? Actually, you can do |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
36 |
\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
|
37 |
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
|
38 |
code during the lectures because its functional |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
39 |
programming-style allows me to implement some of the functions |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
40 |
we will discuss with very small and elegant code. Since the |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
41 |
compiler is free, you can download it and run every example I |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
42 |
give. But if you prefer, you can also translate the examples |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
43 |
into any other functional language, for example Haskell, ML, |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
44 |
F\# and so on. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
46 |
Writing programs in Scala can be done with the Eclipse IDE and |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
47 |
also with IntelliJ, but for the small programs we will look at |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
48 |
the Emacs-editor id good for me and I will run programs on the |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
49 |
command line. One advantage of Scala over Java is that it |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
50 |
includes an interpreter (a REPL, or Read-Eval-Print-Loop) with |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
51 |
which you can run and test small code-snippets without the |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
52 |
need of the compiler. This helps a lot for interactively |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
developing programs. Once you installed Scala correctly, you |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
can start the interpreter by typing |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
56 |
|
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
\begin{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
$ scala\small |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
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
|
60 |
Type in expressions to have them evaluated. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
Type :help for more information.\normalsize |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
scala> |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
\end{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
66 |
\noindent The precise output may vary due to the platform |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
67 |
where you installed Scala. At the scala prompt you can type |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
68 |
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
|
69 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
\begin{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
scala> 2 + 3 |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
res0: Int = 5 |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
\end{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
75 |
\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
|
76 |
type {\tt Int} and the actual result is {\tt 5}. Another |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
77 |
example you can type in is |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
\begin{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
scala> print ("hello world") |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
hello world |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
\end{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
\noindent which prints out a string. Note that in this case |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
there is no result: the reason is that {\tt print} does not |
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
86 |
actually produce a result (there is no {\tt res\_}), rather it |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
87 |
is a function that causes the \emph{side-effect} of printing |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
88 |
out a string. Once you are more familiar with the functional |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
89 |
programming-style, you will know what the difference is |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
90 |
between a function that returns a result, like addition, and a |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
91 |
function that causes a side-effect, like {\tt print}. We shall |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
92 |
come back to this later, but if you are curious, the latter |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
93 |
kind of 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
|
94 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
\subsection*{Inductive Datatypes} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
The elegance and conciseness of Scala programs stems often |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
from the fact that inductive datatypes can be easily defined. |
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
99 |
For example in ``every-day Mathematics'' we would define |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
100 |
regular expressions simply by the grammar |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
\begin{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
\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
|
104 |
$r$ & $::=$ & $\varnothing$ & null\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
& $\mid$ & $\epsilon$ & empty string\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
& $\mid$ & $c$ & single character\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
& $\mid$ & $r_1 \cdot r_2$ & sequence\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
& $\mid$ & $r_1 + r_2$ & alternative / choice\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
& $\mid$ & $r^*$ & star (zero or more)\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
\end{tabular} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |
\end{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
113 |
\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
|
114 |
(essentially a kind of tree-structure with three kinds of |
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
115 |
inner nodes and three kinds of leave nodes). If you are |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
116 |
familiar with Java, it might be an instructive exercise to |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
117 |
define this kind of inductive datatypes in |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
118 |
Java.\footnote{Happy programming! ;o)} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
119 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
120 |
Implementing the regular expressions from above in Scala is |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
121 |
very simple: It first requires an \emph{abstract class}, say, |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
122 |
{\tt Rexp}. This will act as type for regular expressions. |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
123 |
Second, it requires some instances. The cases for |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
124 |
$\varnothing$ and $\epsilon$ do not have any arguments, while |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
125 |
in the other cases we do have arguments. For example the |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
126 |
character regular expression needs to take as an argument the |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
127 |
character it is supposed to recognise. In Scala, the cases |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
128 |
without arguments are called \emph{case objects}, while the |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
129 |
ones with arguments are \emph{case classes}. The corresponding |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
130 |
code is as follows: |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
131 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
132 |
\begin{quote} |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
133 |
\begin{lstlisting}[language=Scala] |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
134 |
abstract class Rexp |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
135 |
case object NULL extends Rexp |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
136 |
case object EMPTY extends Rexp |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
137 |
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
|
138 |
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
|
139 |
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
|
140 |
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
|
141 |
\end{lstlisting} |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
142 |
\end{quote} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
143 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
144 |
\noindent Given the grammar above, I hope you can see the |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
145 |
underlying pattern. In order to be an instance of {\tt Rexp}, |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
146 |
each case object or case class needs to extend {\tt Rexp}. If |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
147 |
you want to play with such definitions, feel free to define |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
148 |
for example binary trees. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
149 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
150 |
Once you make a definition like the one above, you can |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
151 |
represent, say, the regular expression for $a + b$ as |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
152 |
{\tt ALT(CHAR('a'), CHAR('b'))}. If you want to assign |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
153 |
this regular expression to a variable, you can just type |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
154 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
155 |
\begin{alltt} |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
156 |
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
|
157 |
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
|
158 |
\end{alltt} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
159 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
160 |
\noindent In order to make such assignments there is no |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
161 |
constructor need in the class (like in Java). However, if |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
162 |
there is the need, you can of course define such a constructor |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
163 |
in Scala. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
164 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
165 |
Note that Scala says the variable {\tt r} is of type {\tt |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
166 |
ALT}, not {\tt Rexp}. Scala always tries to find the most |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
167 |
general type that is needed for a variable, but does not |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
168 |
``over-generalise''. In this case there is no need to give |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
169 |
{\tt r} the more general type of {\tt Rexp}. This is different |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
170 |
if you want to form a list of regular expressions, for example |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
171 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
172 |
\begin{alltt} |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
173 |
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
|
174 |
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
|
175 |
\end{alltt} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
176 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
177 |
\noindent In this case Scala needs to assign a type to the |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
178 |
regular expressions, so that it is compatible with the fact |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
179 |
that list can only contain elements of a single type, in this |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
180 |
case this is {\tt Rexp}.\footnote{If you type in this example, |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
181 |
you will notice that the type contains some further |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
182 |
information, but lets ignore this for the moment.} Note that if a type takes another |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
183 |
type as argument, this is written for example as |
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
184 |
{\tt List[Rexp]}. |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
185 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
186 |
\subsection*{Functions and Pattern-Matching} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
187 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
188 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
189 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
190 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
191 |
\subsection*{Types} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
192 |
|
228
4df4404455d0
more on scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
193 |
\subsection*{Cool Stuff} |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
194 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
195 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
196 |
\end{document} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
197 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
198 |
%%% Local Variables: |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
199 |
%%% mode: latex |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
200 |
%%% TeX-master: t |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
201 |
%%% End: |