--- a/README Fri Oct 05 11:23:15 2018 +0100
+++ b/README Fri Oct 05 11:25:53 2018 +0100
@@ -1,4 +1,19 @@
+given two lists, is one a sublist of the other except one
+element
+check Graham's Haskell book
+
+five-in-a-row as a spiral
+(Tic-Tac-Toe on steriods or Go for the weak mind)
+
+
+----------
+CokeBottle cokeBottle = new CokeBottle();
+
+joke from...
+
+https://www.youtube.com/watch?v=9e_oEE72d3U
+----------
CW8A
(1) There is an ontime submission (with full marks) by
--- a/cws/cw03.tex Fri Oct 05 11:23:15 2018 +0100
+++ b/cws/cw03.tex Fri Oct 05 11:25:53 2018 +0100
@@ -76,6 +76,8 @@
\begin{document}
+% BF IDE
+% https://www.microsoft.com/en-us/p/brainf-ck/9nblgggzhvq5
\section*{Coursework 8 (Regular Expressions and Brainf***)}
--- a/handouts/pep-ho.tex Fri Oct 05 11:23:15 2018 +0100
+++ b/handouts/pep-ho.tex Fri Oct 05 11:25:53 2018 +0100
@@ -10,6 +10,22 @@
% case class, apply, unapply
% see https://medium.com/@thejasbabu/scala-pattern-matching-9c9e73ba9a8a
+% the art of programming
+% https://www.youtube.com/watch?v=QdVFvsCWXrA
+
+% functional programming in Scala
+%https://www.amazon.com/gp/product/1449311032/ref=as_li_ss_tl?ie=UTF8&tag=aleottshompag-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1449311032
+
+% functional programmming in C
+%https://www.amazon.com/gp/product/0201419505/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=0201419505&linkCode=as2&tag=aleottshompag-20
+
+%speeding through haskell
+%https://openlibra.com/en/book/download/speeding-through-haskell
+
+% fp books --- ocaml
+% http://courses.cms.caltech.edu/cs134/cs134b/book.pdf
+% http://alexott.net/en/fp/books/
+
\begin{document}
\fnote{\copyright{} Christian Urban, King's College London, 2017, 2018}
@@ -36,8 +52,8 @@
``Scala is the better Java''.\footnote{from
\url{https://www.slideshare.net/maximnovak/joy-of-scala}}
-A number of companies---the Guardian, Twitter, Coursera, FourSquare, Netflix,
-LinkedIn to name a few---either use Scala exclusively in
+A number of companies---the Guardian, Twitter, Coursera, FourSquare,
+Netflix, LinkedIn, ITV to name a few---either use Scala exclusively in
production code, or at least to some substantial degree. Scala seems
also useful in job-interviews (especially in data science) according to
this anecdotal report
@@ -51,12 +67,18 @@
\begin{quote}
\url{http://www.scala-lang.org}
-\end{quote}
+\end{quote}
\noindent
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. It can be downloaded for free from
+obviously Windows.\footnote{Unlike \emph{Microsoft Visual Studio}---note
+the minuscule difference in the name---which is a heavy-duty,
+Windows-only IDE\ldots{}jeez, with all their money could they not come
+up with a completely different name for a complete different project?
+For the pedantic, Microsoft Visual Studio is an IDE, whereas Visual
+Studio Code is a source code editor. Anybody knows the what the
+difference is?} It can be downloaded for free from
\begin{quote}
\url{https://code.visualstudio.com}
@@ -64,7 +86,8 @@
\noindent
and should already come pre-installed in the Department (together with
-the Scala compiler). VS Code is far from perfect, however it includes a
+the Scala compiler). Being a project 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
Figure~\ref{vscode} for my setup).
@@ -105,8 +128,8 @@
\end{quote}
\noindent
-Also IntelliJ includes plugins for Scala. \textbf{BUT}, I do \textbf{not}
-recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs
+Also IntelliJ includes plugins for Scala. \underline{\textbf{BUT}},
+I do \textbf{not} recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs
seem to make your life harder, rather than easier, for the small
programs we will write in this module. They are really meant to be used
when you have a million-lines codebase, rather than our
@@ -136,10 +159,10 @@
programming seems to go against the core principles of
\textit{imperative programming} (which is what you do in Java and C++
for example). The main idea of imperative programming is that you have
-some form of ``state'' in your program and you continuously change this
-state by issuing some commands (e.g.~updating a field in an array or
-object, adding one to a variable and so on). The classic example for
-this style of programming are \texttt{for}-loops, for example
+some form of \emph{state} in your program and you continuously change this
+state by issuing some commands---for example for updating a field in an
+array or object, or for adding one to a variable and so on. The classic
+example for this style of programming are \texttt{for}-loops, like
\begin{lstlisting}[language=C,numbers=none]
for (int i = 10; i < 20; i++) {
@@ -153,10 +176,10 @@
exits. When this code is compiled and actually runs, there will be some
dedicated space reserved for \texttt{i} in memory. This space of
typically 32 bits contains \texttt{i}'s current value\ldots\texttt{10}
-at the beginning, and then the content will be updated by, or
-overwritten with, some new content in every iteration. The main point
-here is that this kind of updating, or manipulating, memory is
-25.806\ldots or \textbf{THE ROOT OF ALL EVIL}!!
+at the beginning, and then the content will be overwritten with some
+new content in every iteration. The main point here is that this kind of
+updating, or manipulating, memory is 25.806\ldots or \textbf{THE ROOT OF
+ALL EVIL}!!
\begin{center}
\includegraphics[scale=0.25]{../pics/root-of-all-evil.png}
@@ -200,7 +223,7 @@
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 parallelize the resulting programs: if you do not have any
+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
@@ -216,21 +239,21 @@
\begin{figure}[p]
\begin{boxedminipage}{\textwidth}
-A Scala program for generating pretty pictures of the Mandelbrot set
-(\url{https://en.wikipedia.org/wiki/Mandelbrot_set},
-\url{https://www.youtube.com/watch?v=aSg2Db3jF_4}):
+A Scala program for generating pretty pictures of the Mandelbrot set.\smallskip\\
+(See \url{https://en.wikipedia.org/wiki/Mandelbrot_set} or\\
+\phantom{(See }\url{https://www.youtube.com/watch?v=aSg2Db3jF_4}):
\begin{center}
\begin{tabular}{c}
-\includegraphics[scale=0.12]{../pics/mand1.png}
+\includegraphics[scale=0.11]{../pics/mand1.png}\\[-8mm]\mbox{}
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{@{}p{0.45\textwidth}|p{0.45\textwidth}@{}}
- \bf sequential version: & \bf parallel version (on 4 cores):\smallskip\\
+ \bf sequential version: & \bf parallel version on 4 cores:\smallskip\\
- {\hfill\includegraphics[scale=0.12]{../pics/mand4.png}\hfill} &
- {\hfill\includegraphics[scale=0.12]{../pics/mand3.png}\hfill} \\
+ {\hfill\includegraphics[scale=0.11]{../pics/mand4.png}\hfill} &
+ {\hfill\includegraphics[scale=0.11]{../pics/mand3.png}\hfill} \\
{\footnotesize\begin{lstlisting}[xleftmargin=-1mm]
for (y <- (0 until H)) {
@@ -239,11 +262,11 @@
val c = start +
(x * d_x + y * d_y * i)
val iters = iterations(c, max)
- val col =
+ val colour =
if (iters == max) black
else colours(iters % 16)
- pixel(x, y, col)
+ pixel(x, y, colour)
}
viewer.updateUI()
}
@@ -256,25 +279,28 @@
val c = start +
(x * d_x + y * d_y * i)
val iters = iterations(c, max)
- val col =
+ val colour =
if (iters == max) black
else colours(iters % 16)
- pixel(x, y, col)
+ pixel(x, y, colour)
}
viewer.updateUI()
}
-\end{lstlisting}}\\
+\end{lstlisting}}\\[-2mm]
\centering\includegraphics[scale=0.5]{../pics/cpu2.png} &
\centering\includegraphics[scale=0.5]{../pics/cpu1.png}
\end{tabular}
\end{center}
-\caption{The main ``loops'' creating the picture of the Mandelbrot set. The parallel version
-only differs in \texttt{.par} 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, while in the parallel version there is a short sharp burst for
-essentially the same workload.
+\caption{The ``main'' loops in the 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,
+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.
\label{mand}}
\end{boxedminipage}
\end{figure}
@@ -292,10 +318,11 @@
programming ``thing'', there are a few more arguments: a lot of research
in programming languages happens to take place in functional programming
languages. This has resulted in ultra-useful features such as
-pattern-matching, strong type-systems, lazyness, implicits, algebraic
+pattern-matching, strong type-systems, laziness, implicits, algebraic
datatypes to name a few. Imperative languages seem to often lag behind
in adopting them: I know, for example, that Java will at some point in
-the future support pattern-matching, which has been used in SML for at
+the future support pattern-matching, which has been used for example
+in SML for at
least 40(!) years. See
\url{http://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html}.
Also Rust, a C-like programming language that has been developed since
@@ -1133,15 +1160,15 @@
\url{http://scalapuzzlers.com} and
\url{http://latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/}
\end{center}
-
-Even if Scala has been a success in several high-profile
-companies, there is also a company (Yammer) that first used
-Scala in their production code, but then moved away from it.
-Allegedly they did not like the steep learning curve of Scala
-and also that new versions of Scala often introduced
-incompatibilities in old code. In the past two months
-there have also been two forks of the Scala compiler.
-It needs to be seen what the future brings for Scala.
+
+Even if Scala has been a success in several high-profile companies,
+there is also a company (Yammer) that first used Scala in their
+production code, but then moved away from it. Allegedly they did not
+like the steep learning curve of Scala and also that new versions of
+Scala often introduced incompatibilities in old code. Also the Java
+language is lately developing at lightening speed taking on many
+features of Scala and other languages, and it seems even it introduces
+new features on its own.
%So all in all, Scala might not be a great teaching language,
%but I hope this is mitigated by the fact that I never require
@@ -1152,6 +1179,14 @@
%with.
+\subsection*{Conclusion}
+
+\begin{itemize}
+\item no exceptions....there two kinds, one ``global'' exceptions, like
+out of memory (not much can be done about this by the ``individual''
+programmer); and ``local one'' open a file that might not exists - in
+the latter you do not want to use exceptions, but Options
+\end{itemize}
\begin{flushright}\it
There are only two kinds of languages: the ones people complain
--- a/progs/lecture1.scala Fri Oct 05 11:23:15 2018 +0100
+++ b/progs/lecture1.scala Fri Oct 05 11:25:53 2018 +0100
@@ -51,7 +51,7 @@
// some alterative syntax for lists
-1::2::3::Nil
+1 :: 2 :: 3 :: Nil
List(1, 2, 3) ::: List(4, 5, 6)
// Printing/Strings
@@ -126,13 +126,13 @@
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"
+ "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:
- |tired buddies head for home""".stripMargin
+ |tired buddies head for home.""".stripMargin
println(lyrics)
@@ -171,6 +171,9 @@
// If-Conditionals
//=================
+// Scala does not have a then-keyword
+// both if-else branches need to be presents
+
def fact(n: Int) : Int =
if (n == 0) 1 else n * fact(n - 1)
@@ -186,13 +189,13 @@
*/
-def fact2(n: BigInt): BigInt =
+def fact2(n: BigInt) : BigInt =
if (n == 0) 1 else n * fact2(n - 1)
fact2(150)
-def fib(n: Int): Int =
+def fib(n: Int) : Int =
if (n == 0) 1 else
if (n == 1) 1 else fib(n - 1) + fib(n - 2)
@@ -252,6 +255,9 @@
mult_table.sliding(10,10).mkString("\n")
+// the list can also be constructed in any other way
+for (n <- List(10,12,4,5,7,8,10)) yield n * n
+
// with if-predicates
@@ -270,6 +276,14 @@
for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2
+// general pattern
+
+for (x <- ...) yield {
+ // potentially complicated
+ // calculation of a result
+}
+
+
// with only a side-effect (no list is produced),
// has no "yield"
@@ -278,9 +292,6 @@
-
-
-
// concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12)
// (would need to have this wrapped into a function, or
// REPL called with scala -Yrepl-class-based)
@@ -303,7 +314,7 @@
// mutable vs immutable factorial
-def fact_mu(n: Long): Long = {
+def fact_mu(n: Long) : Long = {
var result : Long = 1
var i = 1
while (i <= n) {
@@ -326,8 +337,8 @@
}
}
-test().sum
-println(l1.sum - l2.sum)
+(for (i <- (1 to 10)) yield test().sum).sum
+
// Webpages
//==========
@@ -533,7 +544,7 @@
santa_state("(((((()))))".toList)
santa_imutable("(((((()))))".toList)
-def randomString(length: Int) =
+def randomString(length: Int) : String =
List.fill(length)((40 + scala.util.Random.nextInt(2)).toChar)
--- a/progs/mandelbrot.scala Fri Oct 05 11:23:15 2018 +0100
+++ b/progs/mandelbrot.scala Fri Oct 05 11:25:53 2018 +0100
@@ -97,8 +97,8 @@
val d_x = (end.re - start.re) / W
val d_y = (end.im - start.im) / H
- for (y <- (0 until H)) {
- for (x <- (0 until W)) {
+ for (y <- (0 until H).par) {
+ for (x <- (0 until W).par) {
val c = start +
(x * d_x + y * d_y * i)
@@ -148,12 +148,12 @@
val delta = (exc2 - exc1) * 0.0333
-
+/*
time_needed(
for (n <- (0 to 12))
mandelbrot(exc1 + delta * n,
exc2 - delta * n, 100))
-
+*/
/*
time_needed(
for (n <- (0 to 12))
@@ -162,3 +162,15 @@
*/
+// Larry Paulson's example
+val exl1 = -0.74364990 + 0.13188170 * i
+val exl2 = -0.74291189 + 0.13261971 * i
+
+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))