wsheets/wsh02.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 26 Apr 2024 17:37:56 +0100
changeset 488 5deaf53c2faa
parent 447 f51e593903ac
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
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\documentclass{article}
426
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
     3
\usepackage{../styles/style}
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
     4
\usepackage{../styles/langs}
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
     5
\usepackage{tikz}
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
     6
\usepackage{pgf}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
\usepackage{marvosym}
184
84dc794928de updated
pdated
parents: 183
diff changeset
     8
\usepackage{boxedminipage}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
    10
\lstset{escapeinside={/*!}{!*/}}
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
    11
\newcommand{\annotation}[1]{\hfill\footnotesize{}#1}
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
    12
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
    13
\usepackage{menukeys}
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    14
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    15
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    16
% Exact colors from NB
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    17
\usepackage[breakable]{tcolorbox}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    18
\definecolor{incolor}{HTML}{303F9F}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    19
\definecolor{outcolor}{HTML}{D84315}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    20
\definecolor{cellborder}{HTML}{CFCFCF}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    21
\definecolor{cellbackground}{HTML}{F7F7F7}
334
841727e27252 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    22
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    23
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    24
    
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
\begin{document}
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    26
\fnote{\copyright{} Christian Urban, King's College London, 2022}
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
    27
445
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
    28
\section*{Scala Worksheet 2}
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
    29
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    30
445
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
    31
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
    32
\subsection*{Task 1 (Options)}
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    33
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    34
Get familiar with the return value of functions that can
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    35
``go wrong'':
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
    36
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
    37
\begin{lstlisting}[numbers=none]
445
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
    38
scala> List(7,2,3,4,5,6).find(_ < 4)
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
    39
scala> List(5,6,7,8,9).find(_ < 4)
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    40
scala> List(5,6,7,8,9).min
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    41
scala> List(5,6,7,8,9).minOption
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    42
scala> List[Int]().minOption
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    45
\noindent
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    46
Note that there needs to be a type-annotation for \texttt{List()} otherwise
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    47
Scala will not know which \texttt{min}-version it should use. 
445
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
    48
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    49
\subsection*{Task 2 (Try)}
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    50
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    51
The Scala-Idiom \texttt{Try-getOrElse} allows you to conveniently
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    52
deal with failure cases.
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    53
445
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
    54
\begin{lstlisting}[numbers=none]
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    55
scala> Try(Some(List(5,6,7,8,9).min)).getOrElse(None)
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    56
scala> Try(Some(List[Int]().min)).getOrElse(None)
445
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
    57
\end{lstlisting}
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    58
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    59
\noindent
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    60
Note that \texttt{Try} needs the library \texttt{scala.util.\_} to be
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    61
imported. 
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    62
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
    63
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
    64
\begin{lstlisting}[numbers=none]
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    65
def safe_div(x: Int, y: Int) : Option[Int] = 
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    66
  Try(Some(x / y)).getOrElse(None)
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    67
\end{lstlisting}
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    68
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    69
\subsection*{Task 3 (URLs / Files)}
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    70
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    71
For simple tasks such as reading webpages and files, Scala provides
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    72
convenient functions \texttt{Source.fromURL} and \texttt{Source.fromFile}.
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    73
To try them out, you need to import \texttt{io.Source}.
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    74
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    75
\begin{lstlisting}[numbers=none]
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    76
scala> Source.fromURL(my_url)("ISO-8859-1").mkString
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    77
scala> Source.fromFile(my_file)("ISO-8859-1").mkString
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    78
\end{lstlisting}
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    79
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    80
\noindent
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    81
These functions return an iterator, which can be transformed into a String
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    82
using \texttt{mkString}. The second argument fixes the character encoding
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    83
and should not be omitted. If you are interested in the individual lines
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    84
in the file, for example, you can use
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    85
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    86
\begin{lstlisting}[numbers=none]
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    87
Source.fromFile(my_file)("ISO-8859-1")
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    88
                            .getLines().toList
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    89
\end{lstlisting}
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    90
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    91
\noindent
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    92
If you are after proper error-handling, then you can use Scala's options
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    93
as follows
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    94
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    95
\begin{lstlisting}[numbers=none]
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    96
Try(Some(Source.fromFile("test.txt")("ISO-8859-1")
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    97
                          .mkString)).getOrElse(None)
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    98
\end{lstlisting}  
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
    99
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   100
This can also be written slightly shorter as
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   101
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   102
\begin{lstlisting}[numbers=none]
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   103
Try(Source.fromFile("test.txt")("ISO-8859-1")
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   104
                          .mkString).toOption
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   105
\end{lstlisting}  
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   106
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   107
\noindent
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   108
In case of reading files, there can be an issue with closing
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   109
files properly. For this Scala provides \texttt{Using}
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   110
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   111
\begin{lstlisting}[numbers=none]
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   112
  Using(Source.fromFile("test.txt")("ISO-8859-1"))
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   113
                                 (_.mkString).toOption
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   114
\end{lstlisting}  
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   115
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   116
\noindent
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   117
This closes the files automatically after reading, but otherwise
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   118
behaves as the code shown above: It gives a \texttt{Some} in the
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   119
success case and \texttt{None} in the failure case. However,
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   120
\texttt{Using} requires a function as argument for prescribing
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   121
of what to do with the file content in the success case.
445
b73e7ce91c10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
   122
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   123
\subsection*{Task 4 (Higher-Order Functions)}
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   124
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   125
Higher-Order functions means that Scala allows functions to
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   126
have functions as arguments and also allows functions to
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   127
return functions. Get familiar with the short-hand notation
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   128
for simple functions
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   129
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   130
\begin{lstlisting}[numbers=none]
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   131
scala> List(7,2,3,4,5,6).find(_ < 4)
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   132
scala> List(7,2,3,4,5,6).count(_ % 2 == 0)
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   133
scala> List(7,2,3,4,5,6).sortWith(_ > _)
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   134
scala> List(7,2,3,4,5,6).filter(_ > 4)
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   135
\end{lstlisting}
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   136
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   137
\noindent
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   138
Be aware that this short-hand notation only works for ``smallish'' functions
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   139
and that sometimes Scala cannot figure out the types involved without
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   140
explicit type annotations.
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   141
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   142
\subsection*{Task 5 (Maps)}
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   143
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   144
Get familiar with the map-function for lists, sets etc. It is the
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   145
quintessential higher-order function and frequently used for transforming
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   146
lists.
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   147
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   148
\begin{lstlisting}[numbers=none]
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   149
scala> List(7,2,3,4,5,6).map(n => n * n)
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   150
\end{lstlisting}  
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   151
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   152
\noindent
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   153
Make also sure you see that Scala's \texttt{for}-comprehensions
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   154
are just syntactic sugar for \texttt{map}s. What would this
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   155
expression look like as \texttt{for}-comprehension? What are
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   156
the advantages of \texttt{for}-comprehensions over \texttt{map}s.
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   157
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   158
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   159
\subsection*{Task 6 (Pattern-Matching)}
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   160
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   161
Rewrite the following function using pattern-matching
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   162
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   163
\begin{lstlisting}[numbers=none]
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   164
def my_map(lst: List[Int], f: Int => Int) : List[Int] = {
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   165
 if (lst == Nil) Nil
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   166
 else f(lst.head) :: my_map(lst.tail, f)
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   167
}
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   168
\end{lstlisting}
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   169
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   170
\noindent
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   171
Observe that the type of the function is from \texttt{Int}s to
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   172
\texttt{Int}s, which is written in Scala as type \texttt{Int => Int}.
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   173
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   174
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   175
\subsection*{Task 7 (Web-Crawler, Hard)}
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   176
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   177
Have a look at the web-crawler at the end of \texttt{lecture2.scala}.
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 445
diff changeset
   178
Can you modify it such that every page is only visited once? 
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   179
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
\end{document}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
%%% Local Variables: 
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
%%% mode: latex
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
%%% TeX-master: t
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
%%% End: