updated
authorChristian Urban <urbanc@in.tum.de>
Sat, 03 Nov 2018 11:07:44 +0000
changeset 195 fc3ac7b70a06
parent 194 060b081523de
child 196 c50b074b3047
updated
cws/cw01.pdf
cws/cw01.tex
handouts/pep-ho.pdf
handouts/pep-ho.tex
progs/lecture1.scala
progs/mandelbrot.scala
slides/slides01.pdf
slides/slides01.tex
testing3/bf.scala
Binary file cws/cw01.pdf has changed
--- a/cws/cw01.tex	Fri Oct 05 11:27:16 2018 +0100
+++ b/cws/cw01.tex	Sat Nov 03 11:07:44 2018 +0000
@@ -1,5 +1,6 @@
 \documentclass{article}
 \usepackage{../style}
+\usepackage{disclaimer}
 %%\usepackage{../langs}
 
 \begin{document}
@@ -7,22 +8,19 @@
 \section*{Coursework 6 (Scala)}
  
 This coursework is about Scala and is worth 10\%. The first and second
-part are due on 16 November at 11pm, and the third part on 23 November
+part are due on 15 November at 11pm, and the third part on 20 December
 at 11pm. You are asked to implement three programs about list
 processing and recursion. The third part is more advanced and might
-include material you have not yet seen in the first lecture.
+include material you have not yet seen in the lecture.
 Make sure the files you submit can be processed by just calling
 \texttt{scala <<filename.scala>>}.\bigskip
 
+\IMPORTANT{}
+
+
 \noindent
-\textbf{Important:} Do not use any mutable data structures in your
-submissions! They are not needed. This means you cannot use 
-\texttt{ListBuffer}s, for example. Do not use \texttt{return} in your
-code! It has a different meaning in Scala, than in Java.
-Do not use \texttt{var}! This declares a mutable variable. Make sure the
-functions you submit are defined on the ``top-level'' of Scala, not
-inside a class or object. Also note that the running time of
-each part will be restricted to a maximum of 360 seconds on my laptop.
+Also note that the running time of each part will be restricted to a
+maximum of 60 seconds on my laptop.
 
 
 \subsection*{Disclaimer}
@@ -30,7 +28,18 @@
 It should be understood that the work you submit represents
 your own effort. You have not copied from anyone else. An
 exception is the Scala code I showed during the lectures or
-uploaded to KEATS, which you can freely use.\bigskip
+uploaded to KEATS, which you can freely use. If you want to
+look up anything from the Scala library, the link to the api
+is
+
+\begin{center}
+\url{http://www.scala-lang.org/api/current/}
+\end{center}  
+
+\noindent
+I usually google for things like \texttt{scala} \texttt{library} \texttt{groupBy}
+and click on the link about the Scala Standard Library for more information.
+\bigskip
 
 
 \subsection*{Part 1 (3 Marks)}
@@ -64,15 +73,15 @@
 this will \emph{always} happen for every number greater than
 0.\footnote{While it is relatively easy to test this conjecture with
   particular numbers, it is an interesting open problem to
-  \emph{prove} that the conjecture is true for \emph{all} numbers ($>
-  0$). Paul Erd\"o{}s, a famous mathematician you might have hard
-  about, said about this conjecture: ``Mathematics may not be ready
-  for such problems.'' and also offered a \$500 cash prize for its
-  solution. Jeffrey Lagarias, another mathematician, claimed that
-  based only on known information about this problem, ``this is an
-  extraordinarily difficult problem, completely out of reach of
-  present day mathematics.'' There is also a
-  \href{https://xkcd.com/710/}{xkcd} cartoon about this conjecture
+  \emph{prove} that the conjecture is true for \emph{all} numbers
+  ($> 0$). Paul Erd\"o{}s, a famous mathematician you might have hard
+  about, is quoted to have said about this conjecture: ``Mathematics
+  is not yet ripe enough for such questions.''  and also offered a
+  \$500 cash prize for its solution. Jeffrey Lagarias, another
+  mathematician, claimed that based only on known information about
+  this problem, ``this is an extraordinarily difficult problem,
+  completely out of reach of present day mathematics.'' There is also
+  a \href{https://xkcd.com/710/}{xkcd} cartoon about this conjecture
   (click \href{https://xkcd.com/710/}{here}). If you are able to solve
   this conjecture, you will definitely get famous.}\bigskip
 
Binary file handouts/pep-ho.pdf has changed
--- a/handouts/pep-ho.tex	Fri Oct 05 11:27:16 2018 +0100
+++ b/handouts/pep-ho.tex	Sat Nov 03 11:07:44 2018 +0000
@@ -34,19 +34,17 @@
 \mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled 
 \underline{a}cademic \underline{la}nguage''}\smallskip\\
 \mbox{}\hfill\textit{ --- a joke(?) found on Twitter}\bigskip
- 
+
+
+\subsection*{Introduction}
+
 \noindent
 Scala is a programming language that combines functional and
 object-oriented programming-styles. It has received quite a bit of
 attention in the last five or so years. One reason for this attention is
 that, like the Java programming language, Scala compiles to the Java
 Virtual Machine (JVM) and therefore Scala programs can run under MacOSX,
-Linux and Windows.\footnote{There are also experimental backends of
-Scala for producing code under Android (\url{http://scala-android.org});
-for generating JavaScript code to build browser applications
-\url{(https://www.scala-js.org)}; and there is work under way to
-have a native Scala compiler generating X86-code
-(\url{http://www.scala-native.org}).} Because of this it has also access to
+Linux and Windows. Because of this it has also access to
 the myriads of Java libraries. Unlike Java, however, Scala often allows
 programmers to write very concise and elegant code.  Some therefore say
 ``Scala is the better Java''.\footnote{from
@@ -66,10 +64,19 @@
 The official Scala compiler can be downloaded from
 
 \begin{quote}
-\url{http://www.scala-lang.org}
-\end{quote} 
+\url{http://www.scala-lang.org}\medskip
+\end{quote}
 
 \noindent
+If you are interested there are also experimental backends of Scala
+for producing code under Android (\url{http://scala-android.org}); for
+generating JavaScript code (\url{https://www.scala-js.org}); and there
+is work under way to have a native Scala compiler generating X86-code
+(\url{http://www.scala-native.org}). Though be warned these backends
+are still rather beta or even alpha.
+
+\subsection*{VS Code and Scala}
+
 I found a convenient IDE for writing Scala programs is Microsoft's
 \textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and
 obviously Windows.\footnote{Unlike \emph{Microsoft Visual Studio}---note
@@ -86,7 +93,7 @@
 
 \noindent
 and should already come pre-installed in the Department (together with
-the Scala compiler). Being a project started in 2015, VS Code is
+the Scala compiler). Being a project that just started in 2015, VS Code is
 relatively new and thus far from perfect. However it includes a
 \textit{Marketplace} from which a multitude of extensions can be
 downloaded that make editing and running Scala code a little easier (see
@@ -97,12 +104,13 @@
 \begin{center}  
 \includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{}
 \end{center}
-\caption{My personal installation of VS Code includes the following
-packages from Marketplace: Scala Syntax (official), Code Runner, Code
-Spell Checker, Rewrap and Subtle Match Brackets. I have also bound 
-the keys \keys{Ctrl} \keys{Ret} to the action
-``Run-Selected-Text-In-Active-Terminal'' in order to quickly evaluate
-small code snippets in the Scala REPL.\label{vscode}}
+\caption{My installation of VS Code includes the following
+  packages from Marketplace: \textbf{Scala Syntax (official)} 0.2.0,
+  \textbf{Code Runner} 0.9.5, \textbf{Code Spell Checker} 1.6.10,
+  \textbf{Rewrap} 1.9.1 and \textbf{Subtle Match
+  Brackets} 3.0.0. I have also bound the keys \keys{Ctrl} \keys{Ret} to the
+  action ``Run-Selected-Text-In-Active-Terminal'' in order to quickly
+  evaluate small code snippets in the Scala REPL.\label{vscode}}
 \end{boxedminipage}
 \end{figure}  
 
@@ -110,16 +118,18 @@
 Scala REPL. But if you prefer another editor for coding, it is also
 painless to work with Scala completely on the command line (as you might
 have done with \texttt{g++} in the earlier part of PEP). For the
-lazybones among us, there is even an online editor and environment for
-developing and running Scala programs called \textit{ScalaFiddle}, which
-requires zero setup (assuming you have a browser handy). You can access
-it from: 
+lazybones among us, there are even online editors and environments for
+developing and running Scala programs called \textit{ScalaFiddle}
+and \textit{Scastie}, which
+require zero setup (assuming you have a browser handy). You can access
+them from: 
  
 \begin{quote}
-\url{https://scalafiddle.io}\medskip
+  \url{https://scalafiddle.io}\\
+  \url{https://scastie.scala-lang.org}\medskip
 \end{quote}
   
-
+\noindent
 Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ.
 A ready-made Scala bundle for Eclipse is available from
 
@@ -222,19 +232,20 @@
 being able to debug such code.
 
 The central idea of functional programming is to eliminate any state
-from programs---or at least from the ``interesting bits''. Because then
-it is easy to parallelise the resulting programs: if you do not have any
-state, then once created, all memory content stays unchanged and reads
-to such memory are absolutely safe without the need of any
-synchronisation. An example is given in Figure~\ref{mand} where in the
-absence of the annoying state, Scala makes it very easy to calculate the
-Mandelbrot set on as many cores of your CPU as possible. Why is it so
-easy in this example? Because each pixel in the Mandelbrot set can be
-calculated independently and the calculation does not need to update any
-variable. It is so easy in fact that going from the sequential version
-of the Mandelbrot program to the parallel version can be achieved by
-adding just eight characters---in two places you have to add
-\texttt{.par}. Try the same in C++ or Java!
+from programs---or at least from the ``interesting bits'' of the
+programs. Because then it is easy to parallelise the resulting
+programs: if you do not have any state, then once created, all memory
+content stays unchanged and reads to such memory are absolutely safe
+without the need of any synchronisation. An example is given in
+Figure~\ref{mand} where in the absence of the annoying state, Scala
+makes it very easy to calculate the Mandelbrot set on as many cores of
+your CPU as possible. Why is it so easy in this example? Because each
+pixel in the Mandelbrot set can be calculated independently and the
+calculation does not need to update any variable. It is so easy in
+fact that going from the sequential version of the Mandelbrot program
+to the parallel version can be achieved by adding just eight
+characters---in two places you have to add \texttt{.par}. Try the same
+in C++ or Java!
 
 \begin{figure}[p]
 \begin{boxedminipage}{\textwidth}
@@ -293,14 +304,14 @@
 \centering\includegraphics[scale=0.5]{../pics/cpu1.png}
 \end{tabular}
 \end{center}
-\caption{The ``main'' loops in the Mandelbrot program.
+\caption{The code of the ``main'' loops in my Mandelbrot program.
 The parallel version differs only in \texttt{.par} being added to the
-``ranges'' of the x-y-coordinates. As can be seen from the CPU loads, in
-the sequential versions there is a lower peak for an extended period,
+``ranges'' of the x and y coordinates. As can be seen from the CPU loads, in
+the sequential version there is a lower peak for an extended period,
 while in the parallel version there is a short sharp burst for
 essentially the same workload\ldots{}meaning you get more work done 
-in a shorter amount of time. This \emph{parallelisation} 
-only works reliably in an immutable program.
+in a shorter amount of time. This easy \emph{parallelisation} 
+only works reliably with an immutable program.
 \label{mand}} 
 \end{boxedminipage}
 \end{figure}  
@@ -343,7 +354,7 @@
 
 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
 $ scala
-Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit Server VM, Java 9).
+Welcome to Scala 2.12.7 (Java HotSpot(TM) 64-Bit Server VM, Java 9).
 Type in expressions for evaluation. Or try :help.
 
 scala>
@@ -481,7 +492,7 @@
 
 \noindent
 So it would be a bit absurd to call values as variables...you cannot
-change them; they cannot vary. You might think you can re-assign them like
+change them; they cannot vary. You might think you can reassign them like
 
 \begin{lstlisting}[numbers=none]
 scala> val x = 42
@@ -533,8 +544,8 @@
 function, \code{rty}, should be given. If the body of the function is
 more complex, then it can be enclosed in braces, like above. If it it
 is just a simple expression, like \code{x + 1}, you can omit the
-braces. Very often functions are recursive (call themselves) like
-the venerable factorial function:
+braces. Very often functions are recursive (that is call themselves),
+like the venerable factorial function:
 
 \begin{lstlisting}[numbers=none]
 def fact(n: Int): Int = 
@@ -734,12 +745,12 @@
 important role. Scala is such a language. You have already
 seen built-in types, like \code{Int}, \code{Boolean},
 \code{String} and \code{BigInt}, but also user-defined ones,
-like \code{Rexp}. Unfortunately, types can be a thorny
+like \code{Rexp} (see coursework). Unfortunately, types can be a thorny
 subject, especially in Scala. For example, why do we need to
 give the type to \code{toSet[Int]}, but not to \code{toList}?
 The reason is the power of Scala, which sometimes means it
 cannot infer all necessary typing information. At the
-beginning while getting familiar with Scala, I recommend a
+beginning, while getting familiar with Scala, I recommend a
 ``play-it-by-ear-approach'' to types. Fully understanding
 type-systems, especially complicated ones like in Scala, can
 take a module on their own.\footnote{Still, such a study can
@@ -1111,6 +1122,8 @@
 
 \subsection*{Coursework}
 
+
+
 \subsection*{More Info}
 
 There is much more to Scala than I can possibly describe in
--- a/progs/lecture1.scala	Fri Oct 05 11:27:16 2018 +0100
+++ b/progs/lecture1.scala	Sat Nov 03 11:07:44 2018 +0000
@@ -554,3 +554,41 @@
 
 time_needed(10, santa_state(large_string))
 time_needed(10, santa_imutable(large_string))
+
+
+
+
+
+//=======================
+// equality is structural
+val a = "Dave"
+val b = "Dave"
+
+if (a == b) println("equal") else println("unequal")
+
+
+
+List.fill(10)(math.random)
+
+//------------------------
+
+
+for (a <- 0 to 10) {
+  println(a)
+}
+
+for (a <- 0 until 10) {
+  println(a)
+}
+
+for (a <- 0 until 2; b <- 0 to 2) {
+  
+}
+
+output
+a=0, b=0
+a=0, b=1
+a=0, b=2
+a=1, b=0
+a=1, b=1
+a=1, b=2
--- a/progs/mandelbrot.scala	Fri Oct 05 11:27:16 2018 +0100
+++ b/progs/mandelbrot.scala	Sat Nov 03 11:07:44 2018 +0000
@@ -166,11 +166,11 @@
 val exl1 = -0.74364990 + 0.13188170 * i
 val exl2 = -0.74291189 + 0.13261971 * i
 
-time_needed(mandelbrot(exl1, exl2, 1000))
+//time_needed(mandelbrot(exl1, exl2, 1000))
 
 
 // example by Jorgen Villadsen
 val exj1 = 0.10284 - 0.63275 * i
 val exj2 = 0.11084 - 0.64075 * i
 
-time_needed(mandelbrot(exj1, exj2, 1000))
+//time_needed(mandelbrot(exj1, exj2, 1000))
Binary file slides/slides01.pdf has changed
--- a/slides/slides01.tex	Fri Oct 05 11:27:16 2018 +0100
+++ b/slides/slides01.tex	Sat Nov 03 11:07:44 2018 +0000
@@ -18,7 +18,7 @@
 \newcommand{\bl}[1]{\textcolor{blue}{#1}}     
 
 % beamer stuff 
-\renewcommand{\slidecaption}{PEP (Scala) 01, King's College London}
+\renewcommand{\slidecaption}{PEP (Scala) \liningnums{01}, King's College London}
 
 
 \begin{document}
@@ -28,20 +28,23 @@
 \frametitle{%
   \begin{tabular}{@ {}c@ {}}
   \\[5mm]
-  \huge PEP Scala (1) 
+  \huge PEP Scala (\liningnums{1}) 
   \end{tabular}}
 
   \normalsize
   \begin{center}
   \begin{tabular}{ll}
     Email:  & christian.urban at kcl.ac.uk\\
-    Office: & N7.07 (North Wing, Bush House)\\
+    Office: & N\liningnums{7.07} (North Wing, Bush House)\\
     Slides \& Code: & KEATS\medskip\\
     Scala Office & \\
-    Hours: &  Thursdays 11 -- 13\\
+    Hours: &  Mondays 12:00 -- 14:00\\
   \end{tabular}
   \end{center}
 
+  \tiny
+  developed since 2004 bvy Martin Odersky
+  picture about assignments
 
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
--- a/testing3/bf.scala	Fri Oct 05 11:27:16 2018 +0100
+++ b/testing3/bf.scala	Sat Nov 03 11:07:44 2018 +0000
@@ -254,7 +254,7 @@
 }
 
 
-compile_run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
+compile("test.bf", """++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
        ..+++.>>.<-.<.+++.------.--------.>>+.>++.""")
 
 compile_run("""++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[