handouts/scala-ho.tex
changeset 235 bc460179148c
parent 234 bf7eecc9cefe
child 236 34e901c529ce
--- a/handouts/scala-ho.tex	Sat Aug 30 00:41:52 2014 +0100
+++ b/handouts/scala-ho.tex	Sun Aug 31 16:25:17 2014 +0100
@@ -45,12 +45,12 @@
 lectures because its functional programming-style allows me to
 implement the functions we will discuss with very small
 code-snippets. If I had to do this in Java, for example, I
-would first have to run through heaps of boilerplate code.
-Since the Scala compiler is free, you can download the
-code-snippets and run every example I give. But if you prefer,
-you can also easily translate them into any other functional
-language, for example Haskell, Standard ML, F$^\#$, Ocaml and
-so on.
+would first have to run through heaps of boilerplate code and
+the code-snippets would not look pretty. Since the Scala
+compiler is free, you can download the code-snippets and run
+every example I give. But if you prefer, you can also easily
+translate them into any other functional language, for example
+Haskell, Standard ML, F$^\#$, Ocaml and so on.
 
 Developing programs in Scala can be done with the Eclipse IDE
 and also with IntelliJ IDE, but for the small programs I will
@@ -92,14 +92,15 @@
 
 \noindent Note that in this case there is no result. The
 reason is that \code{print} does not actually produce a result
-(there is no \code{resXX}), rather it is a function that
-causes the \emph{side-effect} of printing out a string. Once
-you are more familiar with the functional programming-style,
-you will know what the difference is between a function that
-returns a result, like addition, and a function that causes a
-side-effect, like \code{print}. We shall come back to this
-point later, but if you are curious now, the latter kind of
-functions always have as return type \code{Unit}.
+(there is no \code{resXX} and no type), rather it is a
+function that causes the \emph{side-effect} of printing out a
+string. Once you are more familiar with the functional
+programming-style, you will know what the difference is
+between a function that returns a result, like addition, and a
+function that causes a side-effect, like \code{print}. We
+shall come back to this point later, but if you are curious
+now, the latter kind of functions always have as return type
+\code{Unit}.
 
 If you want to write a stand-alone app in Scala, you can
 implement an object that is an instance of \code{App}, say
@@ -192,9 +193,11 @@
 can represent, for example, the regular expression for $a + b$
 as \code{ALT(CHAR('a'), CHAR('b'))}. Expressions such as
 \code{'a'} stand for ASCII characters, though in the output
-syntax the quotes are omitted. If you want to assign this
-regular expression to a variable, you can use the keyword
-\code{val} and type
+syntax, as you can see below, the quotes are omitted. In a
+later section we will see how we can support the more
+mathematical infix notation for regular expression operators
+in Scala. If you want to assign this regular expression to a
+variable, you can use the keyword \code{val} and type
 
 \begin{lstlisting}[language=Scala,numbers=none]
 scala> val r = ALT(CHAR('a'), CHAR('b'))
@@ -219,13 +222,11 @@
 different if you want to form a list of regular expressions,
 for example
 
-
 \begin{lstlisting}[language=Scala,numbers=none]
 scala> val ls = List(ALT(CHAR('a'), CHAR('b')), NULL)
 ls: List[Rexp] = List(ALT(CHAR(a),CHAR(b)), NULL)
 \end{lstlisting}
 
-
 \noindent In this case, Scala needs to assign a common type to
 the regular expressions so that it is compatible with the
 fact that lists can only contain elements of a single type. In
@@ -467,7 +468,7 @@
 
 \begin{lstlisting}[language=Scala,numbers=none]
 scala> for (n <- (1 to 4).toList; 
-     c <- ('a' to 'c').toList) yield (n, c)
+            l <- ('a' to 'c').toList) yield (n, l)
 res6 = List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), 
             (3,a), (3,b), (3,c), (4,a), (4,b), (4,c))
 \end{lstlisting}
@@ -610,7 +611,7 @@
 
 
 \begin{lstlisting}[language=Scala, numbers=none]
-def quo_rem(m: Int, n: Int) : (Int, Int) = (m / n, m \% n)
+def quo_rem(m: Int, n: Int) : (Int, Int) = (m / n, m % n)
 \end{lstlisting}
 
 
@@ -634,7 +635,8 @@
 \end{lstlisting}
 
 
-\noindent Unlike other functional programming languages, there
+\noindent It takes an integer as argument and returns a
+string. Unlike other functional programming languages, there
 is in Scala no easy way to find out the types of existing
 functions, except by looking into the documentation
 
@@ -727,8 +729,8 @@
 
 \subsection*{Cool Stuff}
 
-The first wow-moment I had with Scala when I came across the
-following code-snippet for reading a web-page. 
+The first wow-moment I had with Scala was when I came across
+the following code-snippet for reading a web-page. 
 
 
 \begin{lstlisting}[language=Scala, numbers=none]
@@ -743,21 +745,22 @@
 more sophisticated, namely only returns the first 10000
 characters of a webpage in case a ``webpage'' is too large.
 Why is that code-snippet of any interest? Well, try
-implementing reading from a webpage in Java. I also like the
+implementing reading-from-a-webpage in Java. I also like the
 possibility of triple-quoting strings, which I have only seen
 in Scala so far. The idea behind this is that in such a string 
 all characters are interpreted literally---there are no 
 escaped characters, like \verb|\n| for newlines.
 
 My second wow-moment I had with a feature of Scala that other
-functional programming languages do not have. This feature is 
-about implicit type conversions. If you have regular 
-expressions and want to use them for language processing you 
-often want to recognise keywords in a language, for example 
-\code{for}, \code{if}, \code{yield} and so on. But the basic 
-regular expression, \code{CHAR}, can only recognise a single 
-character. In order to recognise a whole string, like \code{
-for}, you have to put many of those together using \code{SEQ}:
+functional programming languages also do not have. This
+feature is about implicit type conversions. If you have
+regular expressions and want to use them for language
+processing you often want to recognise keywords in a language,
+for example \code{for}, \code{if}, \code{yield} and so on. But
+the basic regular expression, \code{CHAR}, can only recognise
+a single character. In order to recognise a whole string, like
+\code{ for}, you have to put many of those together using
+\code{SEQ}:
 
 
 \begin{lstlisting}[language=Scala,numbers=none]
@@ -772,8 +775,9 @@
 regular expression above. But then your code is littered with
 such conversion function.
 
-In Scala you can do better by ``hiding'' the conversion 
-functions. The keyword for doing this is \code{implicit}.
+In Scala you can do better by ``hiding'' the conversion
+functions. The keyword for doing this is \code{implicit} and
+it needs a built-in library called \code{implicitConversions}.
 Consider the code
 
 
@@ -801,13 +805,15 @@
 insert a call to the \code{string2rexp}-function. I can now
 write for example
 
-
 \begin{lstlisting}[language=Scala,numbers=none]
 scala> ALT("ab", "ac")
 res9: ALT = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c)))
 \end{lstlisting}
 
-
+\noindent \code{ALT} expects two regular expressions
+as arguments, but I only supply two strings. The implicit
+conversion function will transform the string into
+a regular expression.
 
 Using implicit definitions, Scala allows me to introduce
 some further syntactic sugar for regular expressions:
@@ -924,7 +930,8 @@
 \noindent Rather than returning \code{false}, this code should
 throw a typing-error. There are also many limitations Scala
 inherited from the JVM that can be really annoying. For
-example a fixed stack size. 
+example a fixed stack size. One can work around this
+particular limitation, but why does one have to?
 
 Even if Scala has been a success in several high-profile
 companies, there is also a company (Yammer) that first used