# HG changeset patch # User Christian Urban # Date 1510305803 0 # Node ID 11396c17cd8b0234eba5ebc6e077c3ad0423bf9f # Parent 6f4d8b5e6d80ae8576cff15e5dcf869d4871929c updated diff -r 6f4d8b5e6d80 -r 11396c17cd8b handouts/pep-ho.pdf Binary file handouts/pep-ho.pdf has changed diff -r 6f4d8b5e6d80 -r 11396c17cd8b handouts/pep-ho.tex --- a/handouts/pep-ho.tex Fri Nov 10 09:00:46 2017 +0000 +++ b/handouts/pep-ho.tex Fri Nov 10 09:23:23 2017 +0000 @@ -588,182 +588,185 @@ like \code{print}. -\subsection*{Cool Stuff} +% \subsection*{Cool Stuff} -The first wow-moment I had with Scala was 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}[ numbers=none] -import io.Source -val url = """http://www.inf.kcl.ac.uk/staff/urbanc/""" -Source.fromURL(url)("ISO-8859-1").take(10000).mkString -\end{lstlisting} +% \begin{lstlisting}[ numbers=none] +% import io.Source +% val url = """http://www.inf.kcl.ac.uk/staff/urbanc/""" +% Source.fromURL(url)("ISO-8859-1").take(10000).mkString +% \end{lstlisting} -\noindent These three lines return a string containing the -HTML-code of my webpage. It actually already does something -more sophisticated, namely only returns the first 10000 -characters of a webpage in case it is too large. Why is that -code-snippet of any interest? Well, try 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. +% \noindent These three lines return a string containing the +% HTML-code of my webpage. It actually already does something +% more sophisticated, namely only returns the first 10000 +% characters of a webpage in case it is too large. Why is that +% code-snippet of any interest? Well, try 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}: +% 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}: -\begin{lstlisting}[numbers=none] -SEQ(CHAR('f'), SEQ(CHAR('o'), CHAR('r'))) -\end{lstlisting} +% \begin{lstlisting}[numbers=none] +% SEQ(CHAR('f'), SEQ(CHAR('o'), CHAR('r'))) +% \end{lstlisting} -\noindent This gets quickly unreadable when the strings and -regular expressions get more complicated. In other functional -programming languages, you can explicitly write a conversion -function that takes a string, say \dq{\pcode{for}}, and -generates the regular expression above. But then your code is -littered with such conversion functions. +% \noindent This gets quickly unreadable when the strings and +% regular expressions get more complicated. In other functional +% programming languages, you can explicitly write a conversion +% function that takes a string, say \dq{\pcode{for}}, and +% generates the regular expression above. But then your code is +% littered with such conversion functions. -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 +% 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 -\begin{lstlisting}[numbers=none] -scala.language.implicitConversions -\end{lstlisting} +% \begin{lstlisting}[numbers=none] +% scala.language.implicitConversions +% \end{lstlisting} -\noindent -Consider the code +% \noindent +% Consider the code -\begin{lstlisting}[language=Scala] -import scala.language.implicitConversions +% \begin{lstlisting}[language=Scala] +% import scala.language.implicitConversions -def charlist2rexp(s: List[Char]) : Rexp = s match { - case Nil => EMPTY - case c::Nil => CHAR(c) - case c::s => SEQ(CHAR(c), charlist2rexp(s)) -} +% def charlist2rexp(s: List[Char]) : Rexp = s match { +% case Nil => EMPTY +% case c::Nil => CHAR(c) +% case c::s => SEQ(CHAR(c), charlist2rexp(s)) +% } -implicit def string2rexp(s: String) : Rexp = - charlist2rexp(s.toList) -\end{lstlisting} +% implicit def string2rexp(s: String) : Rexp = +% charlist2rexp(s.toList) +% \end{lstlisting} -\noindent where the first seven lines implement a function -that given a list of characters generates the corresponding -regular expression. In Lines 9 and 10, this function is used -for transforming a string into a regular expression. Since the -\code{string2rexp}-function is declared as \code{implicit}, -the effect will be that whenever Scala expects a regular -expression, but I only give it a string, it will automatically -insert a call to the \code{string2rexp}-function. I can now -write for example +% \noindent where the first seven lines implement a function +% that given a list of characters generates the corresponding +% regular expression. In Lines 9 and 10, this function is used +% for transforming a string into a regular expression. Since the +% \code{string2rexp}-function is declared as \code{implicit}, +% the effect will be that whenever Scala expects a regular +% expression, but I only give it a string, it will automatically +% insert a call to the \code{string2rexp}-function. I can now +% write for example -\begin{lstlisting}[numbers=none] -scala> ALT("ab", "ac") -res9 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c))) -\end{lstlisting} +% \begin{lstlisting}[numbers=none] +% scala> ALT("ab", "ac") +% res9 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c))) +% \end{lstlisting} -\noindent Recall that \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. +% \noindent Recall that \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: +% Using implicit definitions, Scala allows me to introduce +% some further syntactic sugar for regular expressions: -\begin{lstlisting}[ numbers=none] -implicit def RexpOps(r: Rexp) = new { - def | (s: Rexp) = ALT(r, s) - def ~ (s: Rexp) = SEQ(r, s) - def % = STAR(r) -} +% \begin{lstlisting}[ numbers=none] +% implicit def RexpOps(r: Rexp) = new { +% def | (s: Rexp) = ALT(r, s) +% def ~ (s: Rexp) = SEQ(r, s) +% def % = STAR(r) +% } -implicit def stringOps(s: String) = new { - def | (r: Rexp) = ALT(s, r) - def | (r: String) = ALT(s, r) - def ~ (r: Rexp) = SEQ(s, r) - def ~ (r: String) = SEQ(s, r) - def % = STAR(s) -} -\end{lstlisting} +% implicit def stringOps(s: String) = new { +% def | (r: Rexp) = ALT(s, r) +% def | (r: String) = ALT(s, r) +% def ~ (r: Rexp) = SEQ(s, r) +% def ~ (r: String) = SEQ(s, r) +% def % = STAR(s) +% } +% \end{lstlisting} -\noindent This might seem a bit overly complicated, but its effect is -that I can now write regular expressions such as $ab + ac$ -simply as +% \noindent This might seem a bit overly complicated, but its effect is +% that I can now write regular expressions such as $ab + ac$ +% simply as -\begin{lstlisting}[numbers=none] -scala> "ab" | "ac" -res10 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c))) -\end{lstlisting} +% \begin{lstlisting}[numbers=none] +% scala> "ab" | "ac" +% res10 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c))) +% \end{lstlisting} -\noindent I leave you to figure out what the other -syntactic sugar in the code above stands for. +% \noindent I leave you to figure out what the other +% syntactic sugar in the code above stands for. -One more useful feature of Scala is the ability to define -functions with varying argument lists. This is a feature that -is already present in old languages, like C, but seems to have -been forgotten in the meantime---Java does not have it. In the -context of regular expressions this feature comes in handy: -Say you are fed up with writing many alternatives as +% One more useful feature of Scala is the ability to define +% functions with varying argument lists. This is a feature that +% is already present in old languages, like C, but seems to have +% been forgotten in the meantime---Java does not have it. In the +% context of regular expressions this feature comes in handy: +% Say you are fed up with writing many alternatives as -\begin{lstlisting}[numbers=none] -ALT(..., ALT(..., ALT(..., ...))) -\end{lstlisting} +% \begin{lstlisting}[numbers=none] +% ALT(..., ALT(..., ALT(..., ...))) +% \end{lstlisting} -\noindent To make it difficult, you do not know how deep such -alternatives are nested. So you need something flexible that -can take as many alternatives as needed. In Scala one can -achieve this by adding a \code{*} to the type of an argument. -Consider the code +% \noindent To make it difficult, you do not know how deep such +% alternatives are nested. So you need something flexible that +% can take as many alternatives as needed. In Scala one can +% achieve this by adding a \code{*} to the type of an argument. +% Consider the code -\begin{lstlisting}[language=Scala] -def Alts(rs: List[Rexp]) : Rexp = rs match { - case Nil => NULL - case r::Nil => r - case r::rs => ALT(r, Alts(rs)) -} +% \begin{lstlisting}[language=Scala] +% def Alts(rs: List[Rexp]) : Rexp = rs match { +% case Nil => NULL +% case r::Nil => r +% case r::rs => ALT(r, Alts(rs)) +% } -def ALTS(rs: Rexp*) = Alts(rs.toList) -\end{lstlisting} +% def ALTS(rs: Rexp*) = Alts(rs.toList) +% \end{lstlisting} -\noindent The function in Lines 1 to 5 takes a list of regular -expressions and converts it into an appropriate alternative -regular expression. In Line 7 there is a wrapper for this -function which uses the feature of varying argument lists. The -effect of this code is that I can write the regular -expression for keywords as +% \noindent The function in Lines 1 to 5 takes a list of regular +% expressions and converts it into an appropriate alternative +% regular expression. In Line 7 there is a wrapper for this +% function which uses the feature of varying argument lists. The +% effect of this code is that I can write the regular +% expression for keywords as -\begin{lstlisting}[numbers=none] -ALTS("for", "def", "yield", "implicit", "if", "match", "case") -\end{lstlisting} +% \begin{lstlisting}[numbers=none] +% ALTS("for", "def", "yield", "implicit", "if", "match", "case") +% \end{lstlisting} -\noindent Again I leave it to you to find out how much this -simplifies the regular expression in comparison with if I had -to write this by hand using only the ``plain'' regular -expressions from the inductive datatype. +% \noindent Again I leave it to you to find out how much this +% simplifies the regular expression in comparison with if I had +% to write this by hand using only the ``plain'' regular +% expressions from the inductive datatype. + +\bigskip\noindent +\textit{More TBD.} \subsection*{More Info} diff -r 6f4d8b5e6d80 -r 11396c17cd8b progs/lecture1.scala --- a/progs/lecture1.scala Fri Nov 10 09:00:46 2017 +0000 +++ b/progs/lecture1.scala Fri Nov 10 09:23:23 2017 +0000 @@ -123,16 +123,16 @@ println("""">\n<"""") /* in Java -val lyrics = "Sun dips down, the day has gone \n" + - "Witches, wolves and giants yawn \n" + - "Queen and dragon, troll and gnome \n" + - "Baddy buddies head for home" +val lyrics = "Sun dips down, the day has gone. \n" + + "Witches, wolves and giants yawn. \n" + + "Queen and dragon, troll and gnome: \n" + + "tired buddies head for home" */ -val lyrics = """Sun dips down, the day has gone - |Witches, wolves and giants yawn - |Queen and dragon, troll and gnome - |Baddy buddies head for home""".stripMargin +val lyrics = """Sun dips down, the day has gone. + |Witches, wolves and giants yawn. + |Queen and dragon, troll and gnome: + |tired buddies head for home""".stripMargin println(lyrics) @@ -171,7 +171,7 @@ // If-Conditionals //================= -def fact(n: Int): Int = +def fact(n: Int) : Int = if (n == 0) 1 else n * fact(n - 1) diff -r 6f4d8b5e6d80 -r 11396c17cd8b progs/mandelbrot.scala --- a/progs/mandelbrot.scala Fri Nov 10 09:00:46 2017 +0000 +++ b/progs/mandelbrot.scala Fri Nov 10 09:23:23 2017 +0000 @@ -9,7 +9,7 @@ // complex numbers case class Complex(val a: Double, val b: Double) { - // represents the complex number a + b*i + // represents the complex number a + b * i def +(that: Complex) = Complex(this.a + that.a, this.b + that.b) def -(that: Complex) = Complex(this.a - that.a, this.b - that.b) def *(that: Complex) = Complex(this.a * that.a - this.b * that.b, @@ -86,9 +86,9 @@ } pixel(x0, y0, iters(Complex(0, 0), 0)) - } - viewer.updateUI() - } + } + viewer.updateUI() + } } // Examples