handouts/scala-ho.tex
changeset 315 470922b46a63
parent 247 84b4bc6e8554
child 316 76678ad5079d
equal deleted inserted replaced
314:7dd5797a5ffa 315:470922b46a63
   221 \noindent In this case, Scala needs to assign a common type to
   221 \noindent In this case, Scala needs to assign a common type to
   222 the regular expressions so that it is compatible with the
   222 the regular expressions so that it is compatible with the
   223 fact that lists can only contain elements of a single type. In
   223 fact that lists can only contain elements of a single type. In
   224 this case the first common type is \code{Rexp}.\footnote{If you
   224 this case the first common type is \code{Rexp}.\footnote{If you
   225 type in this example, you will notice that the type contains
   225 type in this example, you will notice that the type contains
   226 some further information, but lets ignore this for the
   226 some further information, but let us ignore this for the
   227 moment.} 
   227 moment.} 
   228 
   228 
   229 For compound types like \code{List[...]}, the general rule is
   229 For compound types like \code{List[...]}, the general rule is
   230 that when a type takes another type as argument, then this
   230 that when a type takes another type as argument, then this
   231 argument type is written in angle-brackets. This can also
   231 argument type is written in angle-brackets. This can also
   236 
   236 
   237 I mentioned above that Scala is a very elegant programming
   237 I mentioned above that Scala is a very elegant programming
   238 language for the code we will write in this module. This
   238 language for the code we will write in this module. This
   239 elegance mainly stems from the fact that in addition to
   239 elegance mainly stems from the fact that in addition to
   240 inductive datatypes, also functions can be implemented very
   240 inductive datatypes, also functions can be implemented very
   241 easily in Scala. To show you this, lets first consider a
   241 easily in Scala. To show you this, let us first consider a
   242 problem from number theory, called the \emph{Collatz-series},
   242 problem from number theory, called the \emph{Collatz-series},
   243 which corresponds to a famous unsolved problem in
   243 which corresponds to a famous unsolved problem in
   244 mathematics.\footnote{See for example
   244 mathematics.\footnote{See for example
   245 \url{http://mathworld.wolfram.com/CollatzProblem.html}.}
   245 \url{http://mathworld.wolfram.com/CollatzProblem.html}.}
   246 Mathematicians define this series as:
   246 Mathematicians define this series as:
   363 \subsection*{Loops, or the Absence thereof}
   363 \subsection*{Loops, or the Absence thereof}
   364 
   364 
   365 Coming from Java or C, you might be surprised that Scala does
   365 Coming from Java or C, you might be surprised that Scala does
   366 not really have loops. It has instead, what is in functional
   366 not really have loops. It has instead, what is in functional
   367 programming called, \emph{maps}. To illustrate how they work,
   367 programming called, \emph{maps}. To illustrate how they work,
   368 lets assume you have a list of numbers from 1 to 8 and want to
   368 let us assume you have a list of numbers from 1 to 8 and want to
   369 build the list of squares. The list of numbers from 1 to 8 
   369 build the list of squares. The list of numbers from 1 to 8 
   370 can be constructed in Scala as follows:
   370 can be constructed in Scala as follows:
   371 
   371 
   372 \begin{lstlisting}[numbers=none]
   372 \begin{lstlisting}[numbers=none]
   373 scala> (1 to 8).toList
   373 scala> (1 to 8).toList