handouts/pep-ho.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 01 Nov 2023 15:01:32 +0000
changeset 468 c71ae4477e55
parent 467 1b879b3e704e
child 473 be818c5a67d4
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
     1
% !TEX program = xelatex
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\documentclass{article}
423
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
     3
\usepackage{../styles/style}
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
     4
\usepackage{../styles/langs}
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
     5
\usepackage{tikz}
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
     6
\usepackage{pgf}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
\usepackage{marvosym}
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
     8
\usepackage{boxedminipage}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
    10
\lstset{escapeinside={/*!}{!*/}}
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
    11
\newcommand{\annotation}[1]{\hfill\footnotesize{}#1}
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
    12
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
    13
\usepackage{menukeys}
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
    14
\usepackage{emoji}
335
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    15
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
%cheat sheet
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
%http://worldline.github.io/scala-cheatsheet/
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    19
% case class, apply, unapply
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    20
% see https://medium.com/@thejasbabu/scala-pattern-matching-9c9e73ba9a8a
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    21
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    22
% the art of programming
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    23
% https://www.youtube.com/watch?v=QdVFvsCWXrA
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    24
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    25
% functional programming in Scala
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    26
%https://www.amazon.com/gp/product/1449311032/ref=as_li_ss_tl?ie=UTF8&tag=aleottshompag-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1449311032
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    27
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    28
% functional programming in C
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    29
%https://www.amazon.com/gp/product/0201419505/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=0201419505&linkCode=as2&tag=aleottshompag-20
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    30
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    31
%speeding through haskell
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    32
%https://openlibra.com/en/book/download/speeding-through-haskell
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    33
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    34
% fp books --- ocaml
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    35
% http://courses.cms.caltech.edu/cs134/cs134b/book.pdf
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    36
% http://alexott.net/en/fp/books/
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    37
257
71e534e28430 updated
Christian Urban <urbanc@in.tum.de>
parents: 198
diff changeset
    38
%John Hughes’ simple words:
71e534e28430 updated
Christian Urban <urbanc@in.tum.de>
parents: 198
diff changeset
    39
%A combinator is a function which builds program fragments
71e534e28430 updated
Christian Urban <urbanc@in.tum.de>
parents: 198
diff changeset
    40
%from program fragments.
71e534e28430 updated
Christian Urban <urbanc@in.tum.de>
parents: 198
diff changeset
    41
71e534e28430 updated
Christian Urban <urbanc@in.tum.de>
parents: 198
diff changeset
    42
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
    43
%explain graph colouring program (examples from)
264
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
    44
%https://www.metalevel.at/prolog/optimization
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
    45
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
    46
% nice example for map and reduce using Harry potter characters
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
    47
% https://www.matthewgerstman.com/map-filter-reduce/
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
    48
333
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
    49
% interesting talk about differences in Java and Scala
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
    50
% Goto'19 conference ; about differences in type-system
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
    51
% https://www.youtube.com/watch?v=e6n-Ci8V2CM
264
25f3fbc43251 updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
    52
329
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    53
% Timing
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    54
%
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    55
% xs.map(x => (x, xs.count(_==x)))
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    56
%
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    57
% vs  xs.groupBy(identity)
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    58
%
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    59
% first is quadratic, while second is linear.
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    60
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    61
% contrast map with a for loop in imperative languages
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    62
%
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    63
% Let’s use a simple example of calculating sales tax on an array of
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    64
% prices.
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    65
%
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    66
%       const prices = [19.99, 4.95, 25, 3.50];
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    67
%       let new_prices = [];
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    68
%
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    69
%       for(let i=0; i < prices.length; i++) {
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    70
%          new_prices.push(prices[i] * 1.06);
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    71
%       }
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    72
%
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    73
% We can achieve the same results using .map():
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    74
%
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    75
% const prices = [19.99, 4.95, 25, 3.50]; 
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    76
% let new_prices = prices.map(price => price * 1.06);
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    77
%
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    78
% The syntax above is condensed so let’s walk through it a bit. The
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    79
% .map() method takes a callback, which can be thought of as a function.
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    80
% That’s what is between the parentheses. The variable price is the name
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    81
% that will be used to identify each value. Since there’s only one
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    82
% input, we can omit the usual parentheses around the parameters.
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    83
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    84
% potentially a worked example? Tetris in scala.js
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    85
%  
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    86
% https://medium.com/@michael.karen/learning-modern-javascript-with-tetris-92d532bcd057
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    87
%
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    88
% Scala videos
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    89
%    https://www.youtube.com/user/DrMarkCLewis
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    90
334
305e1dcdb3bc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    91
305e1dcdb3bc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    92
%% https://alvinalexander.com/downloads/HelloScala-FreePreview.pdf
305e1dcdb3bc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    93
%% 
305e1dcdb3bc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    94
%% Section 10 about strings; interpolations and multiline strings
305e1dcdb3bc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    95
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    96
% Easy installation
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    97
%https://alexarchambault.github.io/posts/2020-09-21-cs-setup.html
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    98
423
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    99
% scala libraries
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   100
%https://index.scala-lang.org
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   101
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   102
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   103
% Learning functional programming is an opportunity to discover a new
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   104
% way to represent programs, to approach problems, and to think about
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   105
% languages. While programming with a functional language is still
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   106
% fundamentally similar to programming with any other type of language
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   107
% (examples of others being imperative or logic), it represents
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   108
% programs and algorithms through distinct forms of abstraction and
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   109
% gives you a new toolset with which to solve programming
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   110
% problems. Additionally, many of the techniques of functional
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   111
% programming are beginning to permeate new mainstream languages, so
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   112
% taking the time now to develop a thorough understanding of them is
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   113
% an investment which will pay great dividends.
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   114
554278cd4b70 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   115
334
305e1dcdb3bc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
   116
335
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   117
% Exact colors from NB
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   118
\usepackage[breakable]{tcolorbox}
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   119
\definecolor{incolor}{HTML}{303F9F}
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   120
\definecolor{outcolor}{HTML}{D84315}
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   121
\definecolor{cellborder}{HTML}{CFCFCF}
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   122
\definecolor{cellbackground}{HTML}{F7F7F7}
334
305e1dcdb3bc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
   123
335
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   124
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   125
    
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
\begin{document}
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   127
\fnote{\copyright{} Christian Urban, King's College London, 2017, 2018, 2019, 2020, 2021, 2022, 2023}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
335
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   129
%\begin{tcolorbox}[breakable,size=fbox,boxrule=1pt,pad at break*=1mm,colback=cellbackground,colframe=cellborder]
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   130
%  abd
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   131
%\end{tcolorbox}
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   132
125
b91ad3761732 updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   133
\section*{A Crash-Course in Scala}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   135
\mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled 
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   136
\underline{a}cademic \underline{la}nguage''}\smallskip\\
192
cd2a9c969ef2 updated
Christian Urban <urbanc@in.tum.de>
parents: 188
diff changeset
   137
\mbox{}\hfill\textit{ --- a joke(?) found on Twitter}\bigskip
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   138
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   139
\mbox{}\hfill\textit{``Life is too short for \texttt{malloc}.''}\smallskip\\
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   140
\mbox{}\hfill\textit{ --- said Neal Ford at Oscon'13}\;\hr{https://www.youtube.com/watch?v=7aYS9PcAITQ}\bigskip\\ 
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   141
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   142
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   143
\subsection*{Introduction}
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   144
178
55367d499635 updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   145
\noindent
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   146
Scala is a programming language that combines functional and
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   147
object-oriented programming-styles. It has received quite a bit of
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   148
attention in the last five or so years. One reason for this attention is
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   149
that, like the Java programming language, Scala compiles to the Java
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   150
Virtual Machine (JVM) and therefore Scala programs can run under MacOSX,
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   151
Linux and Windows. Because of this it has also access to
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   152
the myriads of Java libraries. Unlike Java, however, Scala often allows
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   153
programmers to write very concise and elegant code.  Some therefore say
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   154
``Scala is the better Java''.\footnote{from
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   155
\url{https://www.slideshare.net/maximnovak/joy-of-scala}} 
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   156
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   157
A number of companies---the Guardian, Dualingo, Coursera, FourSquare,
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   158
Netflix, LinkedIn, ITV to name a few---either use Scala exclusively in
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   159
production code, or at least to some substantial degree. Scala seems
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   160
also useful in job-interviews (especially in data science) according to
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   161
this anecdotal report
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   162
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   163
\begin{quote}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   164
\url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child}
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   165
\end{quote}
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   166
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   167
\noindent
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   168
The official Scala web-page is here:
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   169
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   170
\begin{quote}
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   171
\url{http://www.scala-lang.org}\medskip
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   172
\end{quote}
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   173
395
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
   174
\noindent\alert
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   175
Just make sure you are using the version 3(!) of Scala. This is
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   176
the version I am going to use in the lectures and in the coursework. This
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   177
can be any version of Scala 3.X where $X=\{1,2,3\}$. Also the minor
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   178
number does not matter. Note that this will be the first year I am
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   179
using this newer version -- so some hiccups are bound to happen. Apologies
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   180
in advance!\bigskip
395
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
   181
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   182
\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   183
  I will be using the \textbf{\texttt{scala-cli}} REPL for scala, rather
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   184
  than the ``plain'' Scala REPL. This is a batteries included version of
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   185
  Scala and is easier to use. In fact \texttt{scala-cli} will replace
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   186
  the ``plain'' Scala REPL in future versions. So why not using it now?
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   187
  It can be downloaded from:
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   188
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   189
  \begin{center}
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   190
  \url{https://scala-cli.virtuslab.org}  
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   191
  \end{center}  
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   192
\end{tcolorbox}\medskip  
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   193
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   194
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   195
\noindent
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   196
If you are interested, there are also experimental backend of Scala
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   197
for generating JavaScript code (\url{https://www.scala-js.org}), and
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   198
there is work under way to have a native Scala compiler generating
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   199
X86-code (\url{http://www.scala-native.org}). There are also some
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   200
tricks you can play with Scala programms running as native
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   201
GraalVM~\hr{https://scala-cli.virtuslab.org/docs/cookbooks/native-images/}
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   202
images.  Though be warned these backends are still rather beta or even
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   203
alpha.
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   204
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   205
\subsection*{VS Code and Scala}
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   206
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   207
I found a convenient IDE for writing Scala programs is Microsoft's
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   208
\textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and
269
3ef2542207c4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 265
diff changeset
   209
obviously Windows.\footnote{\ldots{}unlike \emph{Microsoft Visual Studio}---note
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   210
the minuscule difference in the name---which is a heavy-duty,
269
3ef2542207c4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 265
diff changeset
   211
Windows-only IDE\ldots{}jeez, with all their money could they not have come
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   212
up with a completely different name for a complete different project?
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   213
For the pedantic, Microsoft Visual Studio is an IDE, whereas Visual
269
3ef2542207c4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 265
diff changeset
   214
Studio Code is considered to be a \emph{source code editor}. Anybody knows what the
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   215
difference is?} It can be downloaded for free from
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   216
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   217
\begin{quote}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   218
\url{https://code.visualstudio.com}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   219
\end{quote}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   220
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   221
\noindent
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   222
and should already come pre-installed in the Department (together with
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   223
the Scala compiler). Being a project that just started in 2015, VS Code is
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   224
relatively new and therefore far from perfect. However it includes a
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   225
\textit{Marketplace} from which a multitude of extensions can be
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   226
downloaded that make editing and running Scala code a little easier (see
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   227
Figure~\ref{vscode} for my setup).
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   228
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   229
\begin{figure}[t]
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   230
\begin{boxedminipage}{\textwidth}  
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   231
\begin{center}  
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   232
\includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   233
\end{center}
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   234
\caption{My installation of VS Code / Codium includes the 
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   235
  package  \textbf{Scala Syntax (official)} 0.5.7 from Marketplace.
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   236
  I have also bound the keys \keys{Ctrl} \keys{Ret} to the
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   237
  action ``Run-Selected-Text-In-Active-Terminal'' in order to quickly
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   238
  evaluate small code snippets in the Scala REPL. I use Codium's internal
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   239
  terminal to run \texttt{scala-cli} version 1.0.5 which
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   240
  uses Scala 3.3.1.\label{vscode}}
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   241
\end{boxedminipage}
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   242
\end{figure}  
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   243
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   244
Actually \alert last year I switched to VS Codium, which is VS Code
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   245
minus all the telemetry that is normally sent to Microsoft. Apart from
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   246
the telemetry (and Copilot, which you are not supposed to use anyway),
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   247
it works pretty much the same way as the original but is driven by a
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   248
dedicated community, rather than a big company. You can download VS
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   249
Codium from
438
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   250
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   251
\begin{quote}
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   252
\url{https://vscodium.com}
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   253
\end{quote}
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   254
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   255
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   256
What I like most about VS Code/Codium is that it provides easy access
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   257
to the Scala REPL. But if you prefer another editor for coding, it is
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   258
also painless to work with Scala completely on the command line (as
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   259
you might have done with \texttt{g++} in the earlier part of PEP). For
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   260
the lazybones among us, there are even online editors and environments
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   261
for developing and running Scala programs: \textit{Scastie} and
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   262
\textit{ScalaFiddle} are two of them. They require zero setup
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   263
(assuming you have a browser handy). You can access them at
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   264
 
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   265
\begin{quote}
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   266
  \url{https://scastie.scala-lang.org}\\
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   267
  \url{https://scalafiddle.io}\medskip
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   268
\end{quote}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   269
  
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   270
\noindent
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   271
But you should be careful if you use them for your coursework: they
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   272
are meant to play around, not really for serious work. Make
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   273
sure your \texttt{scala-cli} works on your own machine ASAP!
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   274
438
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   275
As one might expect, Scala can be used with the heavy-duty IDEs
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   276
Eclipse and IntelliJ. For example IntelliJ includes plugins for
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   277
Scala
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   278
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   279
\begin{quote}
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   280
\url{https://scalacenter.github.io/bloop/docs/ides/intellij}
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   281
\end{quote}
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   282
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   283
\noindent
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   284
\underline{\textbf{BUT}}, I do \textbf{not} recommend the usage of
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   285
either Eclipse or IntelliJ for PEP: these IDEs seem to make your life
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   286
harder, rather than easier, for the small programs that we will write
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   287
in this module. They are really meant to be used when you have a
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   288
million-lines codebase instead of our small
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   289
``toy-programs''\ldots{}for example why on earth am I required to
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   290
create a completely new project with several subdirectories when I
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   291
just want to try out 20-lines of Scala code? Your mileage may vary
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   292
though.~\texttt{;o)}
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   293
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   294
\subsection*{Why Functional Programming?}
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   295
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   296
Before we go on, let me explain a bit more why we want to inflict upon
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   297
you another programming language. You hopefully have mastered Java and
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   298
C++, possibly Python\ldots{} the world should be your oyster, no?
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   299
Well, matters are not as simple as one might wish. We do require Scala
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   300
in PEP, but actually we do not religiously care whether you learn
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   301
Scala---after all it is just a programming language (albeit a nifty
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   302
one IMHO). What we do care about is that you learn about
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   303
\textit{functional programming}. Scala is just the vehicle for
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   304
that. Still, you need to learn Scala well enough to get good marks in
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   305
PEP, but functional programming could perhaps equally be taught with
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   306
Haskell, F\#, SML, Ocaml, Kotlin, Clojure, Scheme, Elm and many other
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   307
functional programming languages.
333
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   308
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   309
%Your friendly lecturer just
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   310
%happens to like Scala and the Department agreed that it is a good idea
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   311
%to inflict Scala upon you.
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   312
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   313
Very likely writing programs in a functional programming language is
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   314
quite different from what you are used to in your study so far. It
183
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   315
might even be totally alien to you. The reason is that functional
787b09fa6068 updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   316
programming seems to go against the core principles of
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   317
\textit{imperative programming} (which is what you do in Java and
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   318
C/C++). The main idea of imperative programming is that
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   319
you have some form of \emph{state} in your program and you
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   320
continuously change this state by issuing some commands---for example
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   321
for updating a field in an array or for adding one to a variable
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   322
stored in memory and so on. The classic example for this style of
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   323
programming is a \texttt{for}-loop in C/C++.  Consider the snippet:
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   324
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   325
\begin{lstlisting}[language=C,numbers=none]
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   326
for (int i = 10; i < 20; i++) { 
269
3ef2542207c4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 265
diff changeset
   327
      //...do something with i...
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   328
}
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   329
\end{lstlisting}
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   330
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   331
\noindent Here the integer variable \texttt{i} embodies part of the
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   332
state of the program, which is first set to \texttt{10} and then
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   333
increased by one in each loop-iteration until it reaches \texttt{20}
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   334
at which point the loop exits. When this code is compiled and actually
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   335
runs, there will be some dedicated space reserved for \texttt{i} in
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   336
memory. This space of typically 32 bits contains \texttt{i}'s current
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   337
value\ldots\texttt{10} at the beginning, and then the content will be
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   338
overwritten with new content in every iteration. The main point here
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   339
is that this kind of updating, or overwriting, of memory is
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   340
25.806\ldots or \textbf{THE ROOT OF ALL EVIL}!!
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   341
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   342
\begin{center}
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   343
\includegraphics[scale=0.25]{../pics/root-of-all-evil.png}
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   344
\end{center}  
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   345
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   346
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   347
\noindent
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   348
\ldots{}Well, it is perfectly benign if you have a sequential program
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   349
that gets run instruction by instruction...nicely one after another.
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   350
This kind of running code uses a single core of your CPU and goes as
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   351
fast as your CPU frequency, also called clock-speed, allows. The problem
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   352
is that this clock-speed has not much increased over the past decade and
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   353
no dramatic increases are predicted for any time soon. So you are a bit
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   354
stuck. This is unlike previous generations of developers who could rely
278
57b5bba67467 updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
   355
upon the fact that approximately every 2 years their code would run
57b5bba67467 updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
   356
twice as fast  because the clock-speed of their CPUs got twice as fast.
333
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   357
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   358
Unfortunately this does not happen any more nowadays. To get you out of
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   359
this dreadful situation, CPU producers pile more and more cores into
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   360
CPUs in order to make them more powerful and potentially make software
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   361
faster. The task for you as developer is to take somehow advantage of
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   362
these cores by running as much of your code as possible in parallel on
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   363
as many cores you have available (typically 4-8 or even more in modern laptops
333
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   364
and sometimes much more on high-end machines). In this situation
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   365
\textit{mutable} variables like \texttt{i} in the C-code above are evil,
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   366
or at least a major nuisance: Because if you want to distribute some of
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   367
the loop-iterations over several cores that are currently idle in your
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   368
system, you need to be extremely careful about who can read and
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   369
overwrite the variable \texttt{i}.\footnote{If you are of the mistaken
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   370
belief that nothing nasty can happen to \texttt{i} inside the
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   371
\texttt{for}-loop, then you need to go back over the C++ material.}
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   372
Especially the writing operation is critical because you do not want
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   373
that conflicting writes mess about with \texttt{i}. Take my word: an
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   374
untold amount of misery has arisen from this problem. The catch is that
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   375
if you try to solve this problem in C/C++ or Java, and be as defensive
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   376
as possible about reads and writes to \texttt{i}, then you need to
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   377
synchronise access to it. The result is that very often your program
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   378
waits more than it runs, thereby defeating the point of trying to run
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   379
the program in parallel in the first place. If you are less defensive,
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   380
then usually all hell breaks loose by seemingly obtaining random
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   381
results. And forget the idea of being able to debug such code.
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   382
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   383
The central idea of functional programming is to eliminate any state
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   384
from programs---or at least from the ``interesting bits'' of the
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   385
programs. Because then it is easy to parallelise the resulting
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   386
programs: if you do not have any state, then once created, all memory
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   387
content stays unchanged and reads to such memory are absolutely safe
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   388
without the need of any synchronisation. An example is given in
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   389
Figure~\ref{mand} where in the absence of the annoying state, Scala
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   390
makes it very easy to calculate the Mandelbrot set on as many cores of
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   391
your CPU as possible. Why is it so easy in this example? Because each
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   392
pixel in the Mandelbrot set can be calculated independently and the
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   393
calculation does not need to update any variable. It is so easy in
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   394
fact that going from the sequential version of the Mandelbrot program
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   395
to the parallel version can be achieved by adding just eight
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   396
characters---in two places you have to add \texttt{.par}. Try the same
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   397
in C/C++ or Java!
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   398
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   399
\begin{figure}[p]
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   400
\begin{boxedminipage}{\textwidth}
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   401
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   402
A Scala program for generating pretty pictures of the Mandelbrot set.\smallskip\\ 
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   403
(See \url{https://en.wikipedia.org/wiki/Mandelbrot_set} or\\
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   404
\phantom{(See }\url{https://www.youtube.com/watch?v=aSg2Db3jF_4}):
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   405
\begin{center}    
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   406
\begin{tabular}{c}  
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   407
\includegraphics[scale=0.11]{../pics/mand1.png}\\[-8mm]\mbox{}
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   408
\end{tabular}
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   409
\end{center}
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   410
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   411
\begin{center}
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   412
\begin{tabular}{@{}p{0.45\textwidth}|p{0.45\textwidth}@{}}
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   413
  \bf sequential version: & \bf parallel version on 4 cores:\smallskip\\
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   414
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   415
  {\hfill\includegraphics[scale=0.11]{../pics/mand4.png}\hfill} &
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   416
  {\hfill\includegraphics[scale=0.11]{../pics/mand3.png}\hfill} \\
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   417
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   418
{\footnotesize\begin{lstlisting}[xleftmargin=-1mm]
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   419
for (y <- (0 until H)) {
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   420
  for (x <- (0 until W)) {
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   421
    
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   422
    val c = start + 
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   423
      (x * d_x + y * d_y * i)
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   424
    val iters = iterations(c, max) 
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   425
    val colour = 
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   426
      if (iters == max) black 
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   427
      else colours(iters % 16)
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   428
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   429
    pixel(x, y, colour)
186
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   430
  }
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   431
  viewer.updateUI()
7f132502cb37 updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   432
}   
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   433
\end{lstlisting}}   
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   434
& 
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   435
{\footnotesize\begin{lstlisting}[xleftmargin=0mm]
400
bf47dbac07f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 395
diff changeset
   436
for (y <- (0 until H).par) {
bf47dbac07f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 395
diff changeset
   437
  for (x <- (0 until W).par) {
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   438
      
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   439
    val c = start + 
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   440
      (x * d_x + y * d_y * i)
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   441
    val iters = iterations(c, max) 
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   442
    val colour = 
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   443
      if (iters == max) black 
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   444
      else colours(iters % 16)
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   445
  
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   446
    pixel(x, y, colour)
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   447
  }
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   448
  viewer.updateUI()
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   449
}   
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   450
\end{lstlisting}}\\[-2mm]
187
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   451
a470dbeaa232 updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   452
\centering\includegraphics[scale=0.5]{../pics/cpu2.png} &
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   453
\centering\includegraphics[scale=0.5]{../pics/cpu1.png}
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   454
\end{tabular}
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   455
\end{center}
270
38e13601cb1b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   456
\caption{The code of the ``main'' loops in my version of the mandelbrot program.
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   457
The parallel version differs only in \texttt{.par} being added to the
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   458
``ranges'' of the x and y coordinates. As can be seen from the CPU loads, in
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   459
the sequential version there is a lower peak for an extended period,
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   460
while in the parallel version there is a short sharp burst for
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   461
essentially the same workload\ldots{}meaning you get more work done 
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   462
in a shorter amount of time. This easy \emph{parallelisation} 
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   463
only works reliably with an immutable program.
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   464
\label{mand}} 
184
19b1e01054f6 updated
pdated
parents: 183
diff changeset
   465
\end{boxedminipage}
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   466
\end{figure}  
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   467
275
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   468
But remember this easy parallelisation of code requires that we have no
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   469
state in our programs\ldots{}that is no counters like \texttt{i} in
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   470
\texttt{for}-loops. You might then ask, how do I write loops without
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   471
such counters? Well, teaching you that this is possible is one of the
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   472
main points of the Scala-part in PEP. I can assure you it is possible,
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   473
but you have to get your head around it. Once you have mastered this, it
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   474
will be fun to have no state in your programs (a side product is that it
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   475
much easier to debug state-less code and also more often than not easier
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   476
to understand). So have fun with Scala!\footnote{If you are still not
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   477
convinced about the function programming ``thing'', there are a few more
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   478
arguments: a lot of research in programming languages happens to take
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   479
place in functional programming languages. This has resulted in
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   480
ultra-useful features such as pattern-matching, strong type-systems,
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   481
laziness, implicits, algebraic datatypes  to name a few. Imperative
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   482
languages seem to often lag behind in adopting them: I know, for
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   483
example, that Java will at some point in the future support
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   484
pattern-matching, which has been used for example in SML for at least
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   485
40(!) years. See
438
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   486
\url{https://openjdk.org/projects/amber/design-notes/patterns/pattern-matching-for-java}.
275
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   487
Automatic garbage collection was included in Java in 1995; the
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   488
functional language LISP had this already in 1958. Generics were added
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   489
to Java 5 in 2004; the functional language SML had it since 1990.
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   490
Higher-order functions were added to C\# in 2007, to Java 8 in
275
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   491
2014; again LISP had them since 1958. Also Rust, a C-like programming
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   492
language that has been developed since 2010 and is gaining quite some
d2e28432378f updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   493
interest, borrows many ideas from functional programming from
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   494
yesteryear.}\medskip
170
3d760b06befa updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   495
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   496
\noindent
438
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   497
If you need any after-work distractions, you might have fun reading
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   498
the following article about FP (functional programming) --- you
438
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   499
might have to disable your browser cookies though if you want to read
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
   500
it for free. And spoiler alert: This is tongue-in-cheek \texttt{;o)}
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   501
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   502
\begin{quote}
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   503
  \url{https://archive.ph/vrofC}
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   504
\end{quote}
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   505
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
\subsection*{The Very Basics}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   508
Let us get back to Scala: One advantage of Scala over Java is that it
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   509
includes an interpreter (a REPL, or
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
\underline{R}ead-\underline{E}val-\underline{P}rint-\underline{L}oop)
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   511
with which you can run and test small code snippets without the need
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   512
of a compiler. This helps a lot with interactively developing
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   513
programs. It is my preferred way of writing small Scala programs. Once
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   514
you installed Scala, you can start the interpreter by typing on the
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   515
command line:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   516
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   517
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   518
$ scala-cli
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   519
Welcome to Scala 3.3.1 (17.0.8.1, Java OpenJDK 64-Bit Server VM).
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   520
Type in expressions for evaluation. Or try :help.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   522
scala>
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   523
\end{lstlisting}%$
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   524
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
\noindent The precise response may vary depending
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   526
on the version and platform where you installed Scala. Make sure
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   527
\texttt{scala-cli} uses Scala version 3. At the Scala
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
prompt you can type things like \code{2 + 3}\;\keys{Ret} and
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
the output will be
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   531
\begin{lstlisting}[numbers=none,language={}]
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
scala> 2 + 3
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   533
val res0: Int = 5
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   536
\noindent The answer means that he result of the addition is of type
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   537
\code{Int} and the actual result is 5; \code{res0} is a name that
125
b91ad3761732 updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   538
Scala gives automatically to the result. You can reuse this name later
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   539
on, for example
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   540
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   541
\begin{lstlisting}[numbers=none,language={}]
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   542
scala> res0 + 4
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   543
val res1: Int = 9
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   544
\end{lstlisting}
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   545
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   546
\noindent
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   547
Another classic example you can try out is
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   549
\begin{lstlisting}[numbers=none,language={}]
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   550
scala> println("hello world")
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
hello world
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
\noindent Note that in this case there is no result. The
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   555
reason is that \code{println} does not actually produce a result
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   556
(there is no \code{resX} and no type), rather it is a
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   557
function that causes the \emph{side-effect} of printing out a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
string. Once you are more familiar with the functional
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
programming-style, you will know what the difference is
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
between a function that returns a result, like addition, and a
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   561
function that causes a side-effect, like \code{println}. We
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
shall come back to this point later, but if you are curious
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
now, the latter kind of functions always has \code{Unit} as
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   564
return type. It is just not printed by Scala. 
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   566
You can try more examples with the \texttt{scala-cli} REPL, but feel free to
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   567
first guess what the result is (not all answers by Scala are obvious):
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   569
\begin{lstlisting}[numbers=none,language={}]
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
scala> 2 + 2
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
scala> 1 / 2
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
scala> 1.0 / 2
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
scala> 1 / 2.0
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
scala> 1 / 0
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
scala> 1.0 / 0.0
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
scala> true == false
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
scala> true && false
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
scala> 1 > 1.0
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
scala> "12345".length
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   580
scala> List(1,2,1).size
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   581
scala> Set(1,2,1).size
265
2692329287bb updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   582
scala> List(1) == List(1)
2692329287bb updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   583
scala> Array(1) == Array(1)
2692329287bb updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   584
scala> Array(1).sameElements(Array(1))
335
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   585
\end{lstlisting}
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   586
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   587
\noindent
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   588
Also observe carefully what Scala responds in the following 
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   589
three instances involving the constant \lstinline!1!---can 
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   590
you explain the differences?
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   591
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   592
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   593
\begin{lstlisting}[numbers=none]
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   594
scala> 1
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   595
scala> 1L
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   596
scala> 1F
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   597
\end{lstlisting}\smallskip
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   598
335
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   599
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   600
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   601
\noindent
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   602
Please take the Scala REPL seriously: If you want to take advantage of my
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   603
reference implementation for the assignments, you will need to be
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   604
able to ``play around'' with it!
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   605
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   606
\subsection*{Standalone Scala Apps}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   608
If you want to write a standalone app in Scala, you can
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   609
implement a function \texttt{hello} and annotate the tag
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   610
\texttt{@main}. For example write
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   611
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   612
\begin{lstlisting}[numbers=none]
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   613
@main 
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   614
def Hello() = println("hello world")
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   616
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   617
%object Hello extends App {
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   618
%    println("hello world")
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   619
%}
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   620
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   621
\noindent save it in a file, say {\tt hello-world.scala}, and
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   622
then use \texttt{scala-cli run} (which internally compiles the
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   623
scala file and runs it):
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   624
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   625
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   626
$ scala-cli run hello-world.scala
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   627
hello world
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   628
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   629
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   630
\noindent
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   631
Like Java, Scala targets the JVM and consequently
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   632
Scala programs can also be executed by the bog-standard Java
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   633
Runtime. This can be done as follows:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   634
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   635
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   636
$ scala-cli --power package --assembly hello-world.scala
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   637
$ java -jar Hello.jar
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   638
hello world
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   639
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   640
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   641
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   642
\subsection*{Values}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   643
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   644
\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   645
  Do not use \code{var} in your code for PEP! This declares a mutable variable.
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   646
  Only use \code{val}!
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   647
\end{tcolorbox}\medskip  
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   648
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   649
\noindent
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   650
In the lectures I will try to avoid as much as possible the term
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   651
\emph{variables} familiar from other programming languages. The reason
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   652
is that Scala has \emph{values}, which can be seen as abbreviations of
271
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   653
larger expressions. The keyword for defining values is \code{val}.
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   654
For example
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   655
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   656
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   657
scala> val x = 42
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   658
val x: Int = 42
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   659
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   660
scala> val y = 3 + 4
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   661
val y: Int = 7
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   662
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   663
scala> val z = x / y
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   664
val z: Int = 6
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   665
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   666
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   667
\noindent
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   668
As can be seen, we first define \code{x} and {y} with admittedly some silly
271
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   669
expressions, and then reuse these values in the definition of \code{z}.
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   670
All easy, right? Why the kerfuffle about values? Well, values are
271
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   671
\emph{immutable}. You cannot change their value after you defined them.
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   672
If you try to reassign \code{z} above, Scala will yell at you:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   673
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   674
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   675
scala> z = 9
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   676
-- [E052] Type Error: -----------------------------------
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   677
1 |z = 9
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   678
  |^^^^^
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   679
  |Reassignment to val z
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   680
  | ...
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   681
1 error found
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   682
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   683
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   684
\noindent
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   685
So it would be a bit absurd to call values as variables...you cannot
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   686
change them; they cannot vary. You might think you can reassign them like
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   687
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   688
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   689
scala> val x = 42
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   690
scala> val z = x / 7
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   691
scala> val x = 70
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   692
scala> println(z) 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   693
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   694
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   695
\noindent but try to guess what Scala will print out 
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   696
for \code{z}?  Will it be \code{6} or \code{10}? A final word about
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   697
values: Try to stick to the convention that names of values should be
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   698
lower case, like \code{x}, \code{y}, \code{foo41} and so on. Upper-case
271
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   699
names you should reserve for what is called \emph{constructors}. And 
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   700
forgive me when I call values as variables\ldots{}it is just something that
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   701
has been in imprinted into my developer-DNA during my early days and
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   702
is difficult to get rid of.~\texttt{;o)}  
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   703
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   704
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   705
\subsection*{Function Definitions}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   706
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   707
We do functional programming! So defining functions will be our main occupation.
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   708
As an example, a function named \code{f} taking a single argument of type 
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   709
\code{Int} can be defined in Scala as follows:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   710
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   711
\begin{lstlisting}[numbers=none]
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   712
def f(x: Int) : String = ...EXPR...
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   713
\end{lstlisting} 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   714
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   715
\noindent
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   716
This function returns the value resulting from evaluating the expression
271
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   717
\code{EXPR} (whatever is substituted for this). Since we declared
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   718
\code{String}, the result of this function will be of type
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   719
\code{String}. It is a good habit to always include this information
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   720
about the return type, while it is only strictly necessary to give this
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   721
type in recursive functions. Simple examples of Scala functions are:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   722
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   723
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   724
def incr(x: Int) : Int = x + 1
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   725
def double(x: Int) : Int = x + x
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   726
def square(x: Int) : Int = x * x
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   727
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   728
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   729
\noindent
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   730
The general scheme for a function is
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   731
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   732
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   733
def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
271
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   734
  ...BODY...
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   735
}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   736
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   737
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   738
\noindent
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   739
where each argument, \texttt{arg1}, \texttt{arg2} and so on, requires 
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   740
its type and the result type of the
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   741
function, \code{rty}, should also be given. If the body of the function is
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   742
more complex, then it can be enclosed in braces, like above. If it
124
880e92e5b604 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   743
is just a simple expression, like \code{x + 1}, you can omit the
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   744
braces. Very often functions are recursive (that is call themselves),
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   745
like the venerable factorial function:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   746
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   747
\begin{lstlisting}[numbers=none]
271
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   748
def fact(n: Int) : Int = 
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   749
  if (n == 0) 1 else n * fact(n - 1)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   750
\end{lstlisting}
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   751
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   752
\noindent
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   753
where we have to give the return type \code{Int}. Note we could also
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   754
have written this with braces as
271
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   755
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   756
\begin{lstlisting}[numbers=none]
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   757
def fact(n: Int) : Int = {
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   758
  if (n == 0) 1 
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   759
  else n * fact(n - 1)
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   760
}    
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   761
\end{lstlisting}
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   762
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   763
\noindent
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   764
but this seems a bit overkill for a small function like \code{fact}.
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   765
Notice that I did not use a \code{then}-keyword in the
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   766
\code{if}-statements and that I enclosed the condition inside
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   767
parentheses, like \code{(n == 0)}. Your eyes might hurt to not see an
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   768
\code{else} with an \code{if}, but this has been long established
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   769
syntax for \code{if}-statements. Scala, to my knowledge, was pretty
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   770
unique in that for nearly 20 years of its existence\ldots{}until Scala
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   771
3 came along. While people like me have perfectly adapted to the sight
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   772
of \code{if}s without \code{then}s, it seems the developers of Scala
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   773
caved in to the special eyesight of Gen-Python people and now allow to
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   774
write the same function also as
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   775
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   776
\begin{lstlisting}[numbers=none]
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   777
def fact(n: Int) : Int = {
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   778
  if n == 0 then 1 
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   779
  else n * fact(n - 1)
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   780
}    
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   781
\end{lstlisting}
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   782
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   783
\noindent
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   784
I accept this might look a bit more familiar to beginners of Scala, if
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   785
they come from other languages, like Java or C++. But that we also had
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   786
to get rid in Scala 3 of the familiar \texttt{\{\}}-parentheses is
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   787
completely beyond me. So in Scala 3 the braces are optional and the
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   788
\texttt{fact}-function can even be written as
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   789
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   790
\begin{lstlisting}[numbers=none]
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   791
def fact(n: Int) : Int = 
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   792
     if n == 0 then 1 
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   793
     else n * fact(n - 1)
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   794
\end{lstlisting}
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   795
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   796
\noindent on the condition that indents now become \emph{meaningful}
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   797
(as in Python).\raisebox{-0.7mm}{\emoji{face-vomiting}} All versions are now permitted in Scala 3. While you
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   798
are free to use any syntax version you want in PEP, or even mix them,
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   799
I will \textbf{not} show you any of my code in the newfangled
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   800
Pythonesque meaningful-indent-syntax. When necessary, I will always
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   801
use braces to indicate the beginning and end of a code block, and I
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   802
have not yet get used to the \code{if}s with
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   803
\code{then}s.\footnote{Scala adopted some very fine features of Python, for example string interpolations, but that we had to completely cave in to
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   804
  the demands of Gen-Python is a step to far for my completely
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   805
  insignificant opinion. For me this is a bridge too far.
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   806
  I always assumed escaping Python's dependency hell
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   807
is every software developers life goal---apparently not. ;o)}
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   808
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   809
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   810
However, no matter which syntax style you adopt for \code{if}s, never
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   811
write an \code{if} without an \code{else}-branch! That has something
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   812
to do with functional programming and you will see its purpose later
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
   813
on. Coming back to function definitions: While \code{def} is the main
335
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   814
mechanism for defining functions, there are a few other ways for doing
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   815
this. We will see some of them in the next sections.
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   816
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   817
Before we go on, let me explain one tricky point in function
335
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   818
definitions, especially in larger definitions. What does a Scala
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   819
function return as result? Scala has a \code{return} keyword, but it is
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   820
used for something different than in Java (and C/C++). Therefore please
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   821
make sure no \code{return} slips into your Scala code.
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   822
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   823
So in the absence of \code{return}, what value does a Scala function
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   824
actually produce? A rule-of-thumb is whatever is in the last line of the
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   825
function is the value that will be returned. Consider the following
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   826
example:\footnote{We could have written this function in just one line,
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   827
but for the sake of argument let's keep the two intermediate values.}
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   828
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   829
\begin{lstlisting}[numbers=none]
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   830
def average(xs: List[Int]) : Int = {
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   831
  val s = xs.sum
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   832
  val n = xs.length
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   833
  s / n
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   834
}    
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   835
\end{lstlisting}
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   836
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   837
\noindent In this example the expression \code{s / n} is in the last
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   838
line of the function---so this will be the result the function
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   839
calculates. The two lines before just calculate intermediate values.
335
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   840
This principle of the ``last-line'' comes in handy when you need to
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   841
print out values, for example, for debugging purposes. Suppose you want
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   842
rewrite the function as
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   843
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   844
\begin{lstlisting}[numbers=none]
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   845
def average(xs: List[Int]) : Int = {
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   846
  val s = xs.sum
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   847
  val n = xs.length
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   848
  val h = xs.head
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   849
  println(s"Input $xs with first element $h")
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   850
  s / n
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   851
}    
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   852
\end{lstlisting}
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   853
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   854
\noindent
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   855
Here the function still only returns the expression \code{s / n} in
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   856
the last line.  The \code{println} before just prints out some
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   857
information about the input of this function, but does not contribute
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   858
to the result of the function. Similarly, the value \code{h} is used
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   859
in the \code{println} but does not contribute to what integer is
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   860
returned by the function.
335
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   861
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   862
A caveat is that the idea with the ``last line'' is only a rough
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   863
rule-of-thumb. A better rule might be: the last expression that is
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   864
evaluated in the function. Consider the following version of
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   865
\code{average}:
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   866
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   867
\begin{lstlisting}[numbers=none]
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   868
def average(xs: List[Int]) : Int = {
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   869
  if (xs.length == 0) 0
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   870
  else xs.sum / xs.length
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   871
}    
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   872
\end{lstlisting}
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   873
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   874
\noindent
335
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   875
What does this function return? Well there are two possibilities: either
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   876
the result of \code{xs.sum / xs.length} in the last line provided the
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   877
list \code{xs} is nonempty, \textbf{or} if the list is empty, then it
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   878
will return \code{0} from the \code{if}-branch (which is technically not
9476aee44eed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   879
the last line, but the last expression evaluated by the function in the
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   880
empty-case).
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   881
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   882
Summing up, do not use \code{return} in your Scala code! A function
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   883
returns what is evaluated by the function as the last expression. There
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   884
is always only one such last expression. Previous expressions might
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   885
calculate intermediate values, but they are not returned. If your
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   886
function is supposed to return multiple things, then one way in Scala is
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   887
to use tuples. For example returning the minimum, average and maximum
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   888
can be achieved by
271
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   889
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   890
\begin{lstlisting}[numbers=none]
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   891
def avr_minmax(xs: List[Int]) : (Int, Int, Int) = {
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   892
  if (xs.length == 0) (0, 0, 0)
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   893
  else (xs.min, xs.sum / xs.length, xs.max)
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   894
}    
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   895
\end{lstlisting}
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   896
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   897
\noindent
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   898
which still satisfies the rule-of-thumb.
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   899
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   900
\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   901
  Do not use \code{return} in your code to indicate what a function
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   902
  produces as a result! It has a different meaning in Scala than in
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   903
  Java. It can change the meaning of your program, and you should
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   904
  never use it.
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   905
\end{tcolorbox}
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   906
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   907
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   908
\subsection*{Loops, or Better the Absence Thereof}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   909
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   910
Coming from Java or C/C++, you might be surprised that Scala does
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   911
not really have loops. It has instead, what is in functional
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   912
programming called, \emph{maps}. To illustrate how they work,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   913
let us assume you have a list of numbers from 1 to 8 and want to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   914
build the list of squares. The list of numbers from 1 to 8 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   915
can be constructed in Scala as follows:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   916
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   917
\begin{lstlisting}[numbers=none,language={}]
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   918
scala> (1 to 8).toList
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   919
val res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   920
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   921
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   922
\noindent Generating from this list the list of corresponding 
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   923
squares in a programming language such as Java, you would assume 
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   924
the list is given as a kind of array. You would then iterate, or loop,
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   925
an index over this array and replace each entry in the array
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   926
by the square. Right? In Scala, and in other functional
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   927
programming languages, you use maps to achieve the same. 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   928
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   929
A map essentially takes a function that describes how each element is
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   930
transformed (in this example the function is $n \rightarrow n * n$) and
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   931
a list over which this function should work. Pictorially you can think
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   932
of the idea behind maps as follows:
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   933
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   934
\begin{center}
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   935
\begin{tikzpicture}
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   936
                      
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   937
  \node (A0) at (1.2,0) {\texttt{List(}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   938
  \node (A1) at (2.0,0) {\texttt{1\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   939
  \node (A2) at (2.9,0) {\texttt{2\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   940
  \node (A3) at (3.8,0) {\texttt{3\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   941
  \node (A4) at (4.7,0) {\texttt{4\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   942
  \node (A5) at (5.6,0) {\texttt{5\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   943
  \node (A6) at (6.5,0) {\texttt{6\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   944
  \node (A7) at (7.4,0) {\texttt{7\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   945
  \node (A8) at (8.3,0) {\texttt{8)}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   946
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   947
  \node (B0) at (1.2,-3) {\texttt{List(}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   948
  \node (B1) at (2.0,-3) {\texttt{1\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   949
  \node (B2) at (3.0,-3) {\texttt{4\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   950
  \node (B3) at (4.1,-3) {\texttt{9\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   951
  \node (B4) at (5.2,-3) {\texttt{16\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   952
  \node (B5) at (6.3,-3) {\texttt{25\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   953
  \node (B6) at (7.4,-3) {\texttt{36\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   954
  \node (B7) at (8.4,-3) {\texttt{49\makebox[0mm]{ ,}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   955
  \node (B8) at (9.4,-3) {\texttt{64\makebox[0mm]{ )}}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   956
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   957
  \draw [->,line width=1mm] (A1.south) -- (B1.north);
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   958
  \draw [->,line width=1mm] (A2.south) -- (B2.north);
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   959
  \draw [->,line width=1mm] (A3.south) -- (B3.north);
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   960
  \draw [->,line width=1mm] (A4.south) -- (B4.north);
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   961
  \draw [->,line width=1mm] (A5.south) -- (B5.north);
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   962
  \draw [->,line width=1mm] (A6.south) -- (B6.north);
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   963
  \draw [->,line width=1mm] (A7.south) -- (B7.north);
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   964
  \draw [->,line width=1mm] (A8.south) -- (B8.north);
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   965
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   966
  \node [red] (Q0) at (-0.3,-0.3) {\large\texttt{n}}; 
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   967
  \node (Q1) at (-0.3,-0.4) {};
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   968
  \node (Q2) at (-0.3,-2.5) {};
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   969
  \node [red] (Q3) at (-0.3,-2.65) {\large\texttt{n\,*\,n}};
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   970
  \draw [->,red,line width=1mm] (Q1.south) -- (Q2.north);
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   971
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   972
  \node [red] at (-1.3,-1.5) {\huge{}\it\textbf{map}};
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   973
 \end{tikzpicture}
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   974
\end{center}
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   975
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   976
\noindent
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   977
On top is the ``input'' list we want to transform; on the left is the
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   978
``map'' function for how to transform each element in the input list
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   979
(the square function in this case); at the bottom is the result list of
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   980
the map. This means that a map generates a \emph{new} list, unlike a
273
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
   981
for-loop in Java or C/C++ which would most likely just update the
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   982
existing list/array.
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   983
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   984
Now there are two ways for expressing such maps in Scala. The first way is
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   985
called a \emph{for-comprehension}. The keywords are \code{for} and
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   986
\code{yield}. Squaring the numbers from 1 to 8 with a for-comprehension
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   987
would look as follows:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   988
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   989
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   990
scala> for (n <- (1 to 8).toList) yield n * n
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   991
val res2: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64)
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   992
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   993
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   994
\noindent  This for-comprehension states that from the list of numbers
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   995
we draw some elements. We use the name \code{n} to range over these
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   996
elements (whereby the name is arbitrary; we could use something more
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   997
descriptive if we wanted to). Using \code{n} we compute the result of
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   998
\code{n * n} after the \code{yield}. This way of writing a map resembles
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   999
a bit the for-loops from imperative languages, even though the ideas
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1000
behind for-loops and for-comprehensions are quite different. Also, this
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1001
is a simple example---what comes after \code{yield} can be a complex
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1002
expression enclosed in \texttt{\{...\}}. A more complicated example
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1003
might be
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1004
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1005
\begin{lstlisting}[numbers=none]
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1006
scala> for (n <- (1 to 8).toList) yield {
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1007
         val i = n + 1
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1008
         val j = n - 1
273
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1009
         i * j + 1
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1010
       }
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1011
val res3: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64)
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1012
\end{lstlisting}
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1013
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1014
Let us come back to the simple example of squaring a list of numbers.
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1015
As you can see in the for-comprehensions above, we specified the list
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1016
where each \code{n} comes from, namely \code{(1 to 8).toList}, and how
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1017
each element needs to be transformed, the expression after the
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1018
\code{yield}. This can also be expressed in a second way in Scala by
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1019
using directly the function \code{map} as follows:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1020
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1021
\begin{lstlisting}[numbers=none,language={}]
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1022
scala> (1 to 8).toList.map(n => n * n)
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1023
val res3 = List(1, 4, 9, 16, 25, 36, 49, 64)
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1024
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1025
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1026
\noindent In this way, the expression \code{n => n * n} stands for the
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1027
function that calculates the square (this is how the \code{n}s are
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1028
transformed by the map).  It might not be obvious, but
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1029
the for-comprehensions above are just syntactic sugar: when compiling such
273
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1030
code, Scala translates for-comprehensions into equivalent maps. This
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1031
even works when for-comprehensions get more complicated (see below).
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1032
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1033
The very charming feature of Scala is that such maps or
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1034
for-comprehensions can be written for any kind of data collection, such
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1035
as lists, sets, vectors, options and so on. For example if we instead
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1036
compute the remainders modulo 3 of this list, we can write
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1037
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1038
\begin{lstlisting}[numbers=none,language={}]
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1039
scala> (1 to 8).toList.map(n => n % 3)
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1040
val res4 = List(1, 2, 0, 1, 2, 0, 1, 2)
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1041
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1042
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1043
\noindent If we, however, transform the numbers 1 to 8 not
270
38e13601cb1b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
  1044
into a list, but into a set, and then compute the remainders
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1045
modulo 3 we obtain
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1046
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1047
\begin{lstlisting}[numbers=none,language={}]
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1048
scala> (1 to 8).toSet.map(n => n % 3)
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1049
val res5 = Set(2, 1, 0)
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1050
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1051
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1052
\noindent This\footnote{This returns actually \code{HashSet(1, 2, 3)},
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1053
but this is just an implementation detail of how sets are implemented in
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1054
Scala.} is the correct result for sets, as there are only three
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1055
equivalence classes of integers modulo 3.  Since maps and
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1056
for-comprehensions are just syntactic variants of each other, the latter
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1057
can also be written as
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1058
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1059
\begin{lstlisting}[numbers=none]
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1060
scala> for (n <- (1 to 8).toSet) yield n % 3
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1061
val res5 = Set(2, 1, 0)
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1062
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1063
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1064
For-comprehensions can also be nested and the selection of 
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1065
elements can be guarded (or filtered). For example if we want to pair up
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1066
the numbers 1 to 4 with the letters a to c, we can write
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1067
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1068
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1069
scala> for (n <- (1 to 4).toList; 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1070
            m <- ('a' to 'c').toList) yield (n, m)
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1071
val res6 = List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), 
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1072
                (3,a), (3,b), (3,c), (4,a), (4,b), (4,c))
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1073
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1074
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1075
\noindent 
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1076
In this example the for-comprehension ranges over two lists, and
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1077
produces a list of pairs as output. Or, if we want to find all pairs of
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1078
numbers between 1 and 3 where the sum is an even number, we can write
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1079
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1080
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1081
scala> for (n <- (1 to 3).toList; 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1082
            m <- (1 to 3).toList;
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1083
            if (n + m) % 2 == 0) yield (n, m)
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1084
val res7 = List((1,1), (1,3), (2,2), (3,1), (3,3))
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1085
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1086
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1087
\noindent The \code{if}-condition in this for-comprehension filters out
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1088
all pairs where the sum is not even (therefore \code{(1, 2)}, \code{(2,
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1089
1)} and \code{(3, 2)} are not in the result because their sum is odd). 
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1090
278
57b5bba67467 updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
  1091
To summarise, maps (or for-comprehensions) transform one collection into
273
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1092
another. For example a list of \code{Int}s into a list of squares, and
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1093
so on. There is no need for for-loops in Scala. But please do not be
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1094
tempted to write anything like
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1095
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1096
\begin{lstlisting}[numbers=none]
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1097
scala> val cs = ('a' to 'h').toList
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1098
scala> for (n <- (0 until cs.length).toList) 
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1099
          yield cs(n).capitalize
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1100
val res8: List[Char] = List(A, B, C, D, E, F, G, H)
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1101
\end{lstlisting}
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1102
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1103
\noindent
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1104
This is accepted Scala-code, but utterly bad style (it is more like
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1105
Java). It can be written much clearer as:
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1106
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1107
\begin{lstlisting}[numbers=none]
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1108
scala> val cs = ('a' to 'h').toList
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1109
scala> for (c <- cs) yield c.capitalize
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1110
val res9: List[Char] = List(A, B, C, D, E, F, G, H)
272
a091de62e3f8 updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1111
\end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1112
271
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
  1113
\subsection*{Results and Side-Effects}
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
  1114
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1115
While hopefully all this about maps looks reasonable, there is one
273
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1116
complication: In the examples above we always wanted to transform one
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1117
list into another list (e.g.~list of squares), or one set into another
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1118
set (set of numbers into set of remainders modulo 3). What happens if we
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1119
just want to print out a list of integers? In these cases the
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1120
for-comprehensions need to be modified. The reason is that \code{print},
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1121
you guessed it, does not produce any result, but only produces what is
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1122
in the functional-programming-lingo called a \emph{side-effect}\ldots it
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1123
prints something out on the screen. Printing out the list of numbers
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1124
from 1 to 5 would look as follows
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1125
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1126
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1127
scala> for (n <- (1 to 5).toList) print(n)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1128
12345
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1129
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1130
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1131
\noindent
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1132
where you need to omit the keyword \code{yield}. You can
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1133
also do more elaborate calculations before printingh such as
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1134
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1135
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1136
scala> for (n <- (1 to 5).toList) {
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1137
  val square = n * n
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1138
  println(s"$n * $n = $square") 
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1139
}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1140
1 * 1 = 1
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1141
2 * 2 = 4
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1142
3 * 3 = 9
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1143
4 * 4 = 16
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1144
5 * 5 = 25
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1145
\end{lstlisting}%$
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1146
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1147
\noindent In this code I use a value assignment (\code{val
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1148
square = ...} ) and also what is called in Scala a
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1149
\emph{string interpolation}, written \code{s"..."}. The latter
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1150
is for printing out formatted strings. It allows me to refer to the
270
38e13601cb1b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
  1151
integer values \code{n} and \code{square} inside a string.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1152
This is very convenient for printing out ``things''. 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1153
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1154
The corresponding map construction for functions with 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1155
side-effects is in Scala called \code{foreach}. So you 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1156
could also write
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1157
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1158
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1159
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1160
scala> (1 to 5).toList.foreach(n => print(n))
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1161
12345
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1162
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1163
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1164
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1165
\noindent or even just
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1166
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1167
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1168
scala> (1 to 5).toList.foreach(print)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1169
12345
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1170
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1171
273
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1172
\noindent 
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1173
If you want to find out more about maps and functions with
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1174
side-effects, you can ponder about the response Scala gives if
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1175
you replace \code{foreach} by \code{map} in the expression
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1176
above. Scala will still allow \code{map} with side-effect
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1177
functions, but then reacts with a slightly interesting result.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1178
273
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1179
\subsection*{Aggregates}
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1180
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1181
There is one more usage of for-loops in Java, C/C++ and the like:
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1182
sometimes you want to \emph{aggregate} something about a list, for
278
57b5bba67467 updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
  1183
example summing up all its elements. In this case you cannot use maps,
273
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1184
because maps \emph{transform} one data collection into another data
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1185
collection. They cannot be used to generate a single integer
278
57b5bba67467 updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
  1186
representing an aggregate. So how is this kind of aggregation done in
57b5bba67467 updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
  1187
Scala? Let us suppose you want to sum up all elements from a list. You
57b5bba67467 updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
  1188
might be tempted to write something like
273
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1189
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1190
\begin{lstlisting}[numbers=none]
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1191
var cnt = 0
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1192
for (n <- (1 to 8).toList) {
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1193
  cnt += n
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1194
}
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1195
print(cnt)
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1196
\end{lstlisting}
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1197
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1198
\noindent
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1199
and indeed this is accepted Scala code and produces the expected result,
273
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1200
namely \code{36}, \textbf{BUT} this is imperative style and not
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1201
permitted in PEP. If you submit this kind of code, you get 0 marks. The
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1202
code uses a \code{var} and therefore violates the immutability property
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1203
I ask for in your code. Sorry!
273
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1204
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1205
So how to do that same thing without using a \code{var}? Well there are
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1206
several ways. One way is to define the following recursive
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1207
\code{sum}-function:
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1208
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1209
\begin{lstlisting}[numbers=none]
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1210
def sum(xs: List[Int]) : Int = 
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1211
  if (xs.isEmpty) 0 else xs.head + sum(xs.tail)
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1212
\end{lstlisting}  
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1213
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1214
\noindent
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1215
You can then call \code{sum((1 to 8).toList)} and obtain the same result
278
57b5bba67467 updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
  1216
without a mutable variable and without a for-loop. Obviously for simple things like
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1217
sum, you could have written \code{xs.sum} in the first place. But not
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1218
all aggregate functions are pre-defined and often you have to write your
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1219
own recursive function for this.
273
9cf4d3c3abd1 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1220
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1221
%\subsection*{Always Produce a Result! No Exceptions!}
329
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1222
%
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1223
%Function should always produce a value. Exception is not thrown.
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1224
%Whenever there is a possibility of non-value result (exception, void,
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1225
%undefined, null, etc.), it should be incorporated in the result type.
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1226
%Such types include but not limited to
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1227
%
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1228
%Option[T]
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1229
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1230
%TBD
334
305e1dcdb3bc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
  1231
329
828326d1b3b2 updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1232
271
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
  1233
\subsection*{Higher-Order Functions}
5c7279e09b15 updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
  1234
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1235
Functions obviously play a central role in functional programming. Two simple
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1236
examples are
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1237
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1238
\begin{lstlisting}[numbers=none]
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1239
def even(x: Int) : Boolean = x % 2 == 0
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1240
def odd(x: Int) : Boolean = x % 2 == 1
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1241
\end{lstlisting} 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1242
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1243
\noindent
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1244
More interestingly, the concept of functions is really pushed to the
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1245
limit in functional programming. Functions can take other functions as
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1246
arguments and can return a function as a result. This is actually
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1247
quite important for making code generic. Assume a list of 10 elements:
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1248
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1249
\begin{lstlisting}[numbers=none]
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1250
val lst = (1 to 10).toList  
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1251
\end{lstlisting} 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1252
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1253
\noindent 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1254
Say, we want to filter out all even numbers. For this we can use 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1255
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1256
\begin{lstlisting}[numbers=none]
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1257
scala> lst.filter(even)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1258
List(2, 4, 6, 8, 10)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1259
\end{lstlisting} 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1260
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1261
\noindent
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1262
where \code{filter} expects a function as argument specifying which
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1263
elements of the list should be kept and which should be left out. By
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1264
allowing \code{filter} to take a function as argument, we can also
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1265
easily filter out odd numbers as well.
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1266
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1267
\begin{lstlisting}[numbers=none]
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1268
scala> lst.filter(odd)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1269
List(1, 3, 5, 7, 9)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1270
\end{lstlisting} 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1271
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1272
\noindent
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1273
Such function arguments are quite frequently used for ``generic'' functions.
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1274
For example it is easy to count odd elements in a list or find the first
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1275
even number in a list:
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1276
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1277
\begin{lstlisting}[numbers=none]
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1278
scala> lst.count(odd)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1279
5
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1280
scala> lst.find(even)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1281
Some(2)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1282
\end{lstlisting} 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1283
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1284
\noindent
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1285
Recall that the return type of \code{even} and \code{odd} are booleans.
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1286
Such function are sometimes called predicates, because they determine
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1287
what should be true for an element and what false, and then performing
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1288
some operation according to this boolean. Such predicates are quite useful. 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1289
Say you want to sort the \code{lst}-list in ascending and descending order. 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1290
For this you can write
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1291
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1292
\begin{lstlisting}[numbers=none]
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1293
lst.sortWith(_ < _)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1294
lst.sortWith(_ > _)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1295
\end{lstlisting} 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1296
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1297
\noindent where \code{sortWith} expects a predicate as argument. The
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1298
construction \code{_ < _} stands for a function that takes two arguments
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1299
and returns true when the first one is smaller than the second. You can
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1300
think of this as elegant shorthand notation for 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1302
\begin{lstlisting}[numbers=none]
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1303
def smaller(x: Int, y: Int) : Boolean = x < y
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1304
lst.sortWith(smaller)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1305
\end{lstlisting} 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1306
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1307
\noindent
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1308
Say you want to find in \code{lst} the first odd number greater than 2.
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1309
For this you need to write a function that specifies exactly this
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1310
condition. To do this you can use a slight variant of the shorthand
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1311
notation above
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1312
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1313
\begin{lstlisting}[numbers=none]
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1314
scala> lst.find(n => odd(n) && n > 2)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1315
Some(3)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1316
\end{lstlisting} 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1317
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1318
\noindent
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1319
Here \code{n => ...} specifies a function that takes \code{n} as
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1320
argument and uses this argument in whatever comes after the double
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1321
arrow. If you want to use this mechanism for looking for an element that
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1322
is both even and odd, then of course you out of luck.
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1323
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1324
\begin{lstlisting}[numbers=none]
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1325
scala> lst.find(n => odd(n) && even(n))
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1326
None
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1327
\end{lstlisting} 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1328
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1329
While functions taking functions as arguments seems a rather useful
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1330
feature, the utility of returning a function might not be so clear. 
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1331
I admit the following example is a bit contrived, but believe me
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1332
sometims functions produce other functions in a very meaningful way.
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1333
Say we want to generate functions according to strings, as in
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1334
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1335
\begin{lstlisting}[numbers=none]
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1336
def mkfn(s: String) : (Int => Boolean) =
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1337
  if (s == "even") even else odd
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1338
\end{lstlisting}
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1339
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1340
\noindent
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1341
With this we can generate the required function for \code{filter}
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1342
according to a string:
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1343
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1344
\begin{lstlisting}[numbers=none]
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1345
scala> lst.filter(mkfn("even"))
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1346
List(2, 4, 6, 8, 10)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1347
scala> lst.filter(mkfn("foo"))
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1348
List(1, 3, 5, 7, 9)
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1349
\end{lstlisting}
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1350
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1351
\noindent
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1352
As said, this is example is a bit contrived---I was not able to think
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1353
of anything simple, but for example in the Compiler module next year I
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1354
show a compilation functions that needs to generate functions as
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1355
intermediate result. Anyway, notice the interesting type we had to
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1356
annotate to \code{mkfn}. The types in Scala are described next.
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1357
274
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
  1358
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1359
\subsection*{Types}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1360
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1361
In most functional programming languages, types play an
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1362
essential role. Scala is such a language. You have already
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1363
seen built-in types, like \code{Int}, \code{Boolean},
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1364
\code{String} and \code{BigInt}, but also user-defined ones,
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1365
like \code{Rexp} (see coursework). Unfortunately, types can be a thorny
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1366
subject, especially in Scala. For example, why do we need to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1367
give the type to \code{toSet[Int]}, but not to \code{toList}?
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1368
The reason is the power of Scala, which sometimes means it
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1369
cannot infer all necessary typing information. At the
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1370
beginning, while getting familiar with Scala, I recommend a
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1371
``play-it-by-ear-approach'' to types. Fully understanding
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1372
type-systems, especially complicated ones like in Scala, can
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1373
take a module on their own.\footnote{Still, such a study can
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1374
be a rewarding training: If you are in the business of
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1375
designing new programming languages, you will not be able to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1376
turn a blind eye to types. They essentially help programmers
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1377
to avoid common programming errors and help with maintaining
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1378
code.}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1379
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1380
In Scala, types are needed whenever you define an inductive
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1381
datatype and also whenever you define functions (their
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1382
arguments and their results need a type). Base types are types
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1383
that do not take any (type)arguments, for example \code{Int}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1384
and \code{String}. Compound types take one or more arguments,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1385
which as seen earlier need to be given in angle-brackets, for
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1386
example \code{List[Int]} or \code{Set[List[String]]} or 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1387
\code{Map[Int, Int]}.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1388
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1389
Scala provides a basic mechanism to check the type of a (closed)
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1390
expression---closed means that all parts are already known to Scala.
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1391
Then you can use the command \code{:type} and check in the REPL:
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1392
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1393
\begin{lstlisting}[ numbers=none]
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1394
scala> :type (1, List(3), Set(4,5), "hello")
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1395
(Int, List[Int], Set[Int], String)
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1396
\end{lstlisting}
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1397
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1398
\noindent
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1399
If Scala can calculate the type of the given expression, then it
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1400
will print it. Unfortunately, this way of finding out a type is almost
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1401
unusable: for `things' where the type is pretty obvious, it gives an
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1402
answer; but for `things' that are actually of interest (such as
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1403
what is the type of a pre-defined function), it gives up with
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1404
an error message. 
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1405
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1406
There are a few special type-constructors that fall outside
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1407
this pattern. One is for tuples, where the type is written
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1408
with parentheses. For example 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1409
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1410
\begin{lstlisting}[ numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1411
(Int, Int, String)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1412
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1413
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1414
\noindent is for a triple (a tuple with three components---two
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1415
integers and a string). Tuples are helpful if you want to
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1416
define functions with multiple results, say the function
270
38e13601cb1b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
  1417
returning the quotient and remainder of two numbers. For this
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1418
you might define:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1419
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1420
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1421
\begin{lstlisting}[ numbers=none]
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1422
def quo_rem(m: Int, n: Int) : (Int, Int) =
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1423
  (m / n, m % n)
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1424
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1425
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1426
\noindent Since this function returns a pair of integers, its
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1427
\emph{return type} needs to be of type \code{(Int, Int)}. Incidentally,
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1428
this is also the \emph{input type} of this function. For this notice
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1429
\code{quo_rem} takes \emph{two} arguments, namely \code{m} and \code{n},
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1430
both of which are integers. They are ``packaged'' in a pair.
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1431
Consequently the complete type of \code{quo_rem} is
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1432
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1433
\begin{lstlisting}[ numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1434
(Int, Int) => (Int, Int)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1435
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1436
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1437
\noindent
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1438
This uses another special type-constructor, written as the arrow
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1439
\code{=>}. This is sometimes also called \emph{function arrow}.  For
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1440
example, the type \code{Int => String} is for a function that takes an
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1441
integer as input argument and produces a string as result.  A function
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1442
of this type is for instance
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1443
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1444
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1445
def mk_string(n: Int) : String = n match {
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1446
  case 0 => "zero"
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1447
  case 1 => "one"
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1448
  case 2 => "two"
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1449
  case _ => "many" 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1450
} 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1451
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1452
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1453
\noindent It takes an integer as input argument and returns a
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1454
string. The type of the function generated in \code{mkfn} above, is
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1455
\code{Int => Boolean}.
277
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1456
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1457
Unfortunately, unlike other functional programming languages, there is
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1458
in Scala no easy way to find out the types of existing functions, except
48dac4856e95 updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1459
by looking into the documentation
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1460
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1461
\begin{quote}
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
  1462
\url{https://dotty.epfl.ch/api/index.html}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1463
\end{quote}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1464
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1465
The function arrow can also be iterated, as in 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1466
\code{Int => String => Boolean}. This is the type for a function
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1467
taking an integer as first argument and a string as second,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1468
and the result of the function is a boolean. Though silly, a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1469
function of this type would be
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1470
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1471
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1472
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1473
def chk_string(n: Int)(s: String) : Boolean = 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1474
  mk_string(n) == s
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1475
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1476
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1477
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1478
\noindent which checks whether the integer \code{n}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1479
corresponds to the name \code{s} given by the function
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1480
\code{mk\_string}. Notice the unusual way of specifying the
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1481
arguments of this function: the arguments are given one after
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1482
the other, instead of being in a pair (what would be the type
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1483
of this function then?). This way of specifying the arguments
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1484
can be useful, for example in situations like this
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1485
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1486
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1487
scala> List("one", "two", "three", "many").map(chk_string(2))
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1488
res4 = List(false, true, false, false)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1489
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1490
scala> List("one", "two", "three", "many").map(chk_string(3))
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1491
res5 = List(false, false, false, true)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1492
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1493
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1494
\noindent In each case we can give to \code{map} a specialised
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1495
version of \code{chk_string}---once specialised to 2 and once
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1496
to 3. This kind of ``specialising'' a function is called
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1497
\emph{partial application}---we have not yet given to this
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1498
function all arguments it needs, but only some of them.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1499
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1500
Coming back to the type \code{Int => String => Boolean}. The
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1501
rule about such function types is that the right-most type
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1502
specifies what the function returns (a boolean in this case).
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1503
The types before that specify how many arguments the function
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1504
expects and what their type is (in this case two arguments,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1505
one of type \code{Int} and another of type \code{String}).
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1506
Given this rule, what kind of function has type
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1507
\mbox{\code{(Int => String) => Boolean}}? Well, it returns a
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1508
boolean. More interestingly, though, it only takes a single
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1509
argument (because of the parentheses). The single argument
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1510
happens to be another function (taking an integer as input and
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1511
returning a string). Remember that \code{mk_string} is just 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1512
such a function. So how can we use it? For this define
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1513
the somewhat silly function \code{apply_3}:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1514
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1515
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1516
def apply_3(f: Int => String): Bool = f(3) == "many"
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1517
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1518
scala> apply_3(mk_string)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1519
res6 = true
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1520
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1521
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1522
You might ask: Apart from silly functions like above, what is
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1523
the point of having functions as input arguments to other
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1524
functions?
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1525
%In Java there is indeed no need of this kind of
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1526
%feature: at least in the past it did not allow such
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1527
%constructions. I think, the point of Java 8 and successors was to lift this
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1528
%restriction.
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1529
Well, in all functional programming languages,
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1530
including Scala, it is really essential to allow functions as
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1531
input argument. Above you have already seen \code{map} and
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1532
\code{foreach} which need this feature. Consider the functions
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1533
\code{print} and \code{println}, which both print out strings,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1534
but the latter adds a line break. You can call \code{foreach}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1535
with either of them and thus changing how, for example, five
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1536
numbers are printed.
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1537
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1538
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1539
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1540
scala> (1 to 5).toList.foreach(print)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1541
12345
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1542
scala> (1 to 5).toList.foreach(println)
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1543
1
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1544
2
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1545
3
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1546
4
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1547
5
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1548
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1549
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1550
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1551
\noindent This is actually one of the main design principles
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1552
in functional programming. You have generic functions like
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1553
\code{map} and \code{foreach} that can traverse data containers,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1554
like lists or sets. They then take a function to specify what
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1555
should be done with each element during the traversal. This
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1556
requires that the generic traversal functions can cope with
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1557
any kind of function (not just functions that, for example,
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1558
take as input an integer and produce a string like above).
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1559
This means we cannot fix the type of the generic traversal
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1560
functions, but have to keep them
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
  1561
\emph{polymorphic}.\footnote{Another interesting topic about
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1562
types, but we omit it here for the sake of brevity.} 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1563
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1564
There is one more type constructor that is rather special. It is
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1565
called \code{Unit}. Recall that \code{Boolean} has two values, namely
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1566
\code{true} and \code{false}. This can be used, for example, to test
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1567
something and decide whether the test succeeds or not. In contrast the
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1568
type \code{Unit} has only a single value, written \code{()}. This
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1569
seems like a completely useless type and return value for a function,
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1570
but is actually quite useful. It indicates when the function does not
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1571
return any result. The purpose of these functions is to cause
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1572
something being written on the screen or written into a file, for
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1573
example. This is what is called they cause a \emph{side-effect}, for
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1574
example new content displayed on the screen or some new data in a
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1575
file. Scala uses the \code{Unit} type to indicate that a function does
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1576
not have a result, but potentially causes a side-effect. Typical
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1577
examples are the printing functions, like \code{print}.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1578
301
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1579
aa0e86419773 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1580
%%\subsection*{User-Defined Types}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1581
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1582
% \subsection*{Cool Stuff}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1583
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1584
% The first wow-moment I had with Scala was when I came across
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1585
% the following code-snippet for reading a web-page. 
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1586
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1587
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1588
% \begin{lstlisting}[ numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1589
% import io.Source
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1590
% val url = """http://www.inf.kcl.ac.uk/staff/urbanc/"""
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1591
% Source.fromURL(url)("ISO-8859-1").take(10000).mkString
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1592
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1593
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1594
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1595
% \noindent These three lines return a string containing the
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1596
% HTML-code of my webpage. It actually already does something
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1597
% more sophisticated, namely only returns the first 10000
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1598
% characters of a webpage in case it is too large. Why is that
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1599
% code-snippet of any interest? Well, try implementing
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1600
% reading-from-a-webpage in Java. I also like the possibility of
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1601
% triple-quoting strings, which I have only seen in Scala so
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1602
% far. The idea behind this is that in such a string all
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1603
% characters are interpreted literally---there are no escaped
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1604
% characters, like \verb|\n| for newlines.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1605
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1606
% My second wow-moment I had with a feature of Scala that other
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1607
% functional programming languages do not have. This feature is
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1608
% about implicit type conversions. If you have regular
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1609
% expressions and want to use them for language processing you
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1610
% often want to recognise keywords in a language, for example
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1611
% \code{for},{} \code{if},{} \code{yield} and so on. But the
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1612
% basic regular expression \code{CHAR} can only recognise a
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1613
% single character. In order to recognise a whole string, like
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1614
% \code{for}, you have to put many of those together using
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1615
% \code{SEQ}:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1616
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1617
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1618
% \begin{lstlisting}[numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1619
% SEQ(CHAR('f'), SEQ(CHAR('o'), CHAR('r')))
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1620
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1621
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1622
% \noindent This gets quickly unreadable when the strings and
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1623
% regular expressions get more complicated. In other functional
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1624
% programming languages, you can explicitly write a conversion
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1625
% function that takes a string, say \dq{\pcode{for}}, and
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1626
% generates the regular expression above. But then your code is
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1627
% littered with such conversion functions.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1628
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1629
% In Scala you can do better by ``hiding'' the conversion
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1630
% functions. The keyword for doing this is \code{implicit} and
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1631
% it needs a built-in library called 
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1632
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1633
% \begin{lstlisting}[numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1634
% scala.language.implicitConversions
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1635
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1636
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1637
% \noindent
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1638
% Consider the code
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1639
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1640
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1641
% \begin{lstlisting}[language=Scala]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1642
% import scala.language.implicitConversions
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1643
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1644
% def charlist2rexp(s: List[Char]) : Rexp = s match {
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1645
%   case Nil => EMPTY
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1646
%   case c::Nil => CHAR(c)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1647
%   case c::s => SEQ(CHAR(c), charlist2rexp(s))
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1648
% }
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1649
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1650
% implicit def string2rexp(s: String) : Rexp = 
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1651
%   charlist2rexp(s.toList)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1652
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1653
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1654
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1655
% \noindent where the first seven lines implement a function
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1656
% that given a list of characters generates the corresponding
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1657
% regular expression. In Lines 9 and 10, this function is used
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1658
% for transforming a string into a regular expression. Since the
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1659
% \code{string2rexp}-function is declared as \code{implicit},
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1660
% the effect will be that whenever Scala expects a regular
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1661
% expression, but I only give it a string, it will automatically
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1662
% insert a call to the \code{string2rexp}-function. I can now
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1663
% write for example
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1664
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1665
% \begin{lstlisting}[numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1666
% scala> ALT("ab", "ac")
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1667
% res9 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c)))
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1668
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1669
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1670
% \noindent Recall that \code{ALT} expects two regular
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1671
% expressions as arguments, but I only supply two strings. The
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1672
% implicit conversion function will transform the string into a
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1673
% regular expression.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1674
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1675
% Using implicit definitions, Scala allows me to introduce
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1676
% some further syntactic sugar for regular expressions:
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1677
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1678
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1679
% \begin{lstlisting}[ numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1680
% implicit def RexpOps(r: Rexp) = new {
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1681
%   def | (s: Rexp) = ALT(r, s)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1682
%   def ~ (s: Rexp) = SEQ(r, s)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1683
%   def % = STAR(r)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1684
% }
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1685
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1686
% implicit def stringOps(s: String) = new {
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1687
%   def | (r: Rexp) = ALT(s, r)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1688
%   def | (r: String) = ALT(s, r)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1689
%   def ~ (r: Rexp) = SEQ(s, r)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1690
%   def ~ (r: String) = SEQ(s, r)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1691
%   def % = STAR(s)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1692
% }
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1693
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1694
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1695
 
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1696
% \noindent This might seem a bit overly complicated, but its effect is
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1697
% that I can now write regular expressions such as $ab + ac$ 
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1698
% simply as
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1699
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1700
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1701
% \begin{lstlisting}[numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1702
% scala> "ab" | "ac"
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1703
% res10 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c)))
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1704
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1705
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1706
 
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1707
% \noindent I leave you to figure out what the other
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1708
% syntactic sugar in the code above stands for.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1709
 
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1710
% One more useful feature of Scala is the ability to define
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1711
% functions with varying argument lists. This is a feature that
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1712
% is already present in old languages, like C, but seems to have
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1713
% been forgotten in the meantime---Java does not have it. In the
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1714
% context of regular expressions this feature comes in handy:
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1715
% Say you are fed up with writing many alternatives as
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1716
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1717
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1718
% \begin{lstlisting}[numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1719
% ALT(..., ALT(..., ALT(..., ...)))
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1720
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1721
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1722
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1723
% \noindent To make it difficult, you do not know how deep such
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1724
% alternatives are nested. So you need something flexible that
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1725
% can take as many alternatives as needed. In Scala one can
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1726
% achieve this by adding a \code{*} to the type of an argument.
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1727
% Consider the code
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1728
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1729
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1730
% \begin{lstlisting}[language=Scala]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1731
% def Alts(rs: List[Rexp]) : Rexp = rs match {
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1732
%   case Nil => NULL
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1733
%   case r::Nil => r
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1734
%   case r::rs => ALT(r, Alts(rs))
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1735
% }
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1736
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1737
% def ALTS(rs: Rexp*) = Alts(rs.toList)
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1738
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1739
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1740
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1741
% \noindent The function in Lines 1 to 5 takes a list of regular
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1742
% expressions and converts it into an appropriate alternative
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1743
% regular expression. In Line 7 there is a wrapper for this
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1744
% function which uses the feature of varying argument lists. The
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1745
% effect of this code  is that I can write the regular
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1746
% expression for keywords as
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1747
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1748
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1749
% \begin{lstlisting}[numbers=none]
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1750
% ALTS("for", "def", "yield", "implicit", "if", "match", "case")
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1751
% \end{lstlisting}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1752
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1753
143
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1754
% \noindent Again I leave it to you to find out how much this
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1755
% simplifies the regular expression in comparison with if I had
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1756
% to write this by hand using only the ``plain'' regular
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1757
% expressions from the inductive datatype.
6f7ec7c531e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1758
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1759
%\bigskip\noindent
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1760
%\textit{More TBD.}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1761
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1762
%\subsection*{Coursework}
181
5b889c263b6b updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
  1763
395
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1764
\begin{figure}[p]
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1765
\begin{boxedminipage}{\textwidth}  
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1766
\textbf{Scala Syntax for Java Developers}\bigskip
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1767
395
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1768
\noindent
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1769
Scala compiles to the JVM, like the Java language. Because of this,
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1770
it can re-use many libraries. Here are a few hints how some Java code
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1771
tranlsates to Scala code:\bigskip
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1772
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1773
\noindent
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1774
Variable declaration:
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1775
\begin{lstlisting}[language=Java]
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1776
Drink coke = getCoke();/*!\annotation{Java}!*/
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1777
\end{lstlisting}
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1778
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1779
\begin{lstlisting}[language=Scala]
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1780
val coke : Drink = getCoke()/*!\annotation{Scala}!*/
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1781
\end{lstlisting}
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1782
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1783
\noindent
395
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1784
or even
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1785
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1786
\begin{lstlisting}[language=Scala]
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1787
val coke = getCoke()/*!\annotation{Scala}!*/
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1788
\end{lstlisting}\bigskip
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1789
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1790
\noindent
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1791
Unit means void:
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1792
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1793
\begin{lstlisting}[language=Java]
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1794
public void output(String s) {/*!\annotation{Java}!*/
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1795
  System.out.println(s);
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1796
}
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1797
\end{lstlisting}
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1798
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1799
\begin{lstlisting}[language=Scala]
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1800
def output(s: String): Unit = println(s)/*!\annotation{Scala}!*/
395
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1801
\end{lstlisting}\bigskip
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1802
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1803
\noindent
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1804
Compound types, say the type for list of Strings:
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1805
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1806
\begin{lstlisting}[language=Java]
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1807
List<String>/*!\annotation{Java}!*/
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1808
\end{lstlisting}
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1809
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1810
\begin{lstlisting}[language=Scala]
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1811
List[String]/*!\annotation{Scala}!*/
395
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1812
\end{lstlisting}\bigskip
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1813
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1814
\noindent
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1815
String interpolations
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1816
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1817
\begin{lstlisting}[language=Java]
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1818
System.out.println("Hello, "+ first + " "+ last + "!");
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1819
/*!\annotation{Java}!*/
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1820
\end{lstlisting}
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1821
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1822
\begin{lstlisting}[language=Scala]
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1823
println(s"Hello, $first $last!")/*!\annotation{Scala}!*/
395
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1824
\end{lstlisting}\bigskip
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1825
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1826
\noindent
395
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1827
Java provides some syntactic sugar when constructing anonymous functions:
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1828
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1829
\begin{lstlisting}[language=Java]
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1830
list.foreach(item -> System.out.println("* " + item));
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1831
/*!\annotation{Java}!*/
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1832
\end{lstlisting}
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1833
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1834
\noindent
438
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
  1835
In Scala, we use the \code{=>} symbol for the same:
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1836
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1837
\begin{lstlisting}[language=Scala]
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1838
list.foreach(item => println(s"* $item"))/*!\annotation{Scala}!*/
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1839
\end{lstlisting}%$
395
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1840
\end{boxedminipage}
e0a82c9f1d21 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1841
\end{figure}
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1842
352
644aca68e203 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1843
%%new / vs case classes
343
51e25cc30483 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1844
195
4bacbe753e66 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1845
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1846
\subsection*{More Info}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1847
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1848
There is much more to Scala than I can possibly describe in
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1849
this short document and teach in the lectures. Fortunately there are a 
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1850
number of free books
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1851
about Scala and of course lots of help online. For example
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1852
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1853
\begin{itemize}
400
bf47dbac07f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 395
diff changeset
  1854
%%\item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf}
bf47dbac07f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 395
diff changeset
  1855
%%\item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1856
\item \url{https://www.youtube.com/user/ShadowofCatron}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1857
\item \url{http://docs.scala-lang.org/tutorials}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1858
\item \url{https://www.scala-exercises.org}
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
  1859
\item \url{https://twitter.github.io/scala_school}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1860
\end{itemize}
188
e075b5015e89 updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
  1861
 
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1862
\noindent There is also an online course at Coursera on Functional
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1863
Programming Principles in Scala by Martin Odersky, the main
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1864
developer of the Scala language. And a document that explains
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1865
Scala for Java programmers
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1866
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1867
\begin{itemize}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1868
\item \small\url{http://docs.scala-lang.org/tutorials/scala-for-java-programmers.html}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1869
\end{itemize}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1870
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1871
While I am quite enthusiastic about Scala, I am also happy to
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1872
admit that it has more than its fair share of faults.
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1873
%The
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1874
%problem seen earlier of having to give an explicit type to
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1875
%\code{toSet}, but not \code{toList} is one of them. There are
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1876
%also many ``deep'' ideas about types in Scala, which even to
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1877
%me as seasoned functional programmer are puzzling.
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1878
For example, whilst
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1879
implicits are great, they can also be a source of great
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1880
headaches, for example consider the code:
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1881
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1882
\begin{lstlisting}[numbers=none]
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1883
scala>  List (1, 2, 3) contains "your mom"
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1884
res1: Boolean = false
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1885
\end{lstlisting}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1886
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1887
\noindent Rather than returning \code{false}, this code should
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1888
throw a typing-error. There are also many limitations Scala
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1889
inherited from the JVM that can be really annoying. For
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1890
example a fixed stack size. One can work around this
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1891
particular limitation, but why does one have to?
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1892
More such `puzzles' can be found at
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1893
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1894
\begin{center}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1895
  \url{http://scalapuzzlers.com} and
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1896
  \url{http://latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1897
\end{center}
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1898
     
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1899
Even if Scala has been a success in several high-profile companies,
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1900
there is also a company (Yammer) that first used Scala in their
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1901
production code, but then moved away from it. Allegedly they did not
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1902
like the steep learning curve of Scala and also that new versions of
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1903
Scala often introduced incompatibilities in old code. Also the Java
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
  1904
language is lately developing at lightening speed (in comparison to
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
  1905
the past) taking on many features of Scala and other languages, and it
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
  1906
seems it even introduces new features on its own. So there is
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
  1907
seemingly even more incentive to stick with the old stuff you
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
  1908
know. Still, the goal of this part of PEP is to bend your mind about
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
  1909
what programming is\ldots{}namely functional programming. I promise
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
  1910
you, this will be useful no matter with which programming language you
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
  1911
will work.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1912
333
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1913
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1914
Scala is deep: After many years, I still continue to learn new technique
467
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1915
for writing more elegant code.
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1916
%Unfortunately, I have not yet managed to
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1917
%switch over my code to Scala 3.0 due to time constraints.
1b879b3e704e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
  1918
Scala 3 seems
438
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
  1919
to iron out a number of snags from Scala 2, but why on earth are they
439
e27ff222fef3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 438
diff changeset
  1920
introducing Python-esque indentation and why on earth are they
438
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
  1921
re-introducing the \texttt{then}-keyword in Scala 3, when I just about got
a02e9efd7bc9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 423
diff changeset
  1922
comfortable without it? 
333
0b82e661341a updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1923
152
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1924
%So all in all, Scala might not be a great teaching language,
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1925
%but I hope this is mitigated by the fact that I never require
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1926
%you to write any Scala code. You only need to be able to read
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1927
%it. In the coursework you can use any programming language you
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1928
%like. If you want to use Scala for this, then be my guest; if
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1929
%you do not want, stick with the language you are most familiar
16dbc95d7d77 updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1930
%with.
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1931
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1932
191
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1933
\subsection*{Conclusion}
48a02384f3ae updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1934
198
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1935
I hope you liked the short journey through the Scala language---but remember we 
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1936
like you to take on board the functional programming point of view,
198
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1937
rather than just learning another language. There is an interesting
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1938
blog article about Scala by a convert:
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1939
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1940
\begin{center}
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1941
\url{https://www.skedulo.com/tech-blog/technology-scala-programming/}
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1942
\end{center}  
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1943
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1944
\noindent
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1945
He makes pretty much the same arguments about functional programming and
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1946
immutability (one section is teasingly called \textit{``Where Did all
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1947
the Bugs Go?''}). If you happen to moan about all the idiotic features
468
c71ae4477e55 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 467
diff changeset
  1948
of Scala (3), well, I guess this is part of the package according to this
198
c2d36d8ee2a7 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1949
quote:\bigskip
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1950
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1951
%\begin{itemize}
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1952
%\item no exceptions....there two kinds, one ``global'' exceptions, like
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1953
%out of memory (not much can be done about this by the ``individual''
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1954
%programmer); and ``local one'' open a file that might not exists - in
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1955
%the latter you do not want to use exceptions, but Options
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1956
%\end{itemize}
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1957
182
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  1958
\begin{flushright}\it
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  1959
There are only two kinds of languages: the ones people complain 
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  1960
about\\ and the ones nobody uses.\smallskip\\
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  1961
\mbox{}\hfill\small{}---Bjarne Stroustrup (the inventor of C++)
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  1962
\end{flushright}
6a4d55584f1f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  1963
123
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1964
\end{document}
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1965
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1966
%%% Local Variables: 
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1967
%%% mode: latex
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1968
%%% TeX-master: t
006f71e905a1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1969
%%% End: