--- a/handouts/pep-ho.tex Fri Feb 23 11:31:36 2024 +0000
+++ b/handouts/pep-ho.tex Fri Apr 26 17:29:30 2024 +0100
@@ -139,13 +139,24 @@
\mbox{}\hfill\textit{``Life is too short for \texttt{malloc}.''}\smallskip\\
\mbox{}\hfill\textit{ --- said Neal Ford at Oscon'13}\;\hr{https://www.youtube.com/watch?v=7aYS9PcAITQ}\bigskip\\
+% In 1982, James or Jim Morris wrote:
+%
+% Functional languages are unnatural to use; but so are knives and
+% forks, diplomatic protocols, double-entry bookkeeping, and a host of
+% other things modern civilization has found useful.
+%
+% Real Programming in Functional Languages
+% Xerox PARC technical report
+% CSL·81·11 July 1,1981
+
+
\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
+attention in the last ten 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. Because of this it has also access to
@@ -155,7 +166,7 @@
\url{https://www.slideshare.net/maximnovak/joy-of-scala}, though this might
be outdated as latest versions of Java are catching up somewhat}
-A number of companies---the Guardian, Dualingo, Coursera, FourSquare,
+A number of companies---the Guardian, Duolingo, 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
@@ -173,11 +184,11 @@
\end{quote}
\noindent\alert
-Just make sure you are using the version 3(!) of Scala. This is
+For PEP, make sure you are using the version 3(!) of Scala. This is
the version I am going to use in the lectures and in the coursework. This
-can be any version of Scala 3.X where $X=\{1,2,3\}$. Also the minor
-number does not matter. Note that this will be the first year I am
-using this newer version -- so some hiccups are bound to happen. Apologies
+can be any version of Scala 3.X where $X=\{3,4\}$. Also the minor
+number does not matter. Note that this will be the second year I am
+using this newer version of Scala -- some hiccups can still happen. Apologies
in advance!\bigskip
\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
@@ -196,7 +207,7 @@
\noindent
-If you are interested, there are also experimental backends of Scala
+If you are interested, there are also experimental backends for Scala
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}). There are also some
@@ -214,7 +225,8 @@
Windows-only IDE\ldots{}jeez, with all their money could they not have 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 considered to be a \emph{source code editor}. Anybody knows what the
+Studio Code is considered to be a \emph{source code editor}. Anybody
+out there knows what the
difference is?} It can be downloaded for free from
\begin{quote}
@@ -259,15 +271,14 @@
What I like most about VS Code/Codium is that it provides easy access
to any 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
+you might do with \texttt{g++} in the second part of PEP). For
the lazybones among us, there are even online editors and environments
-for developing and running Scala programs: \textit{Scastie} and
-\textit{ScalaFiddle} are two of them. They require zero setup
-(assuming you have a browser handy). You can access them at
+for developing and running Scala programs: for example \textit{Scastie}
+is one of them. It requires zero setup
+(assuming you have a browser handy). You can access it at
\begin{quote}
- \url{https://scastie.scala-lang.org}\\
- \url{https://scalafiddle.io}\medskip
+ \url{https://scastie.scala-lang.org}
\end{quote}
\noindent
@@ -298,16 +309,16 @@
Before we go on, let me explain a bit more why we want to inflict upon
you another programming language. You hopefully have mastered Java and
-C++, possibly Python\ldots{} the world should be your oyster, no?
-Well, matters are not as simple as one might wish. We do require Scala
-in PEP, but actually we do not religiously care whether you learn
-Scala---after all it is just a programming language (albeit a nifty
-one IMHO). What we do care about is that you learn about
-\textit{functional programming}. Scala is just the vehicle for
-that. Still, you need to learn Scala well enough to get good marks in
-PEP, but functional programming could perhaps equally be taught with
-Haskell, F\#, SML, Ocaml, Kotlin, Clojure, Scheme, Elm and many other
-functional programming languages.
+soon will master C++ as well, you possibly know Python already\ldots{}
+the world should be your oyster, no? Well, as usual matters are not as simple
+as one might wish. We do require Scala in PEP, but actually we do not
+religiously care whether you learn Scala---after all it is just a
+programming language (albeit a nifty one IMHO). What we do care about
+is that you learn about \textit{functional programming}. Scala is just
+the vehicle for that. Still, you need to learn Scala well enough to
+get good marks in PEP, but functional programming could perhaps
+equally be taught with Haskell, F\#, SML, Ocaml, Kotlin, Clojure,
+Scheme, Elm and many other functional programming languages.
%Your friendly lecturer just
%happens to like Scala and the Department agreed that it is a good idea
@@ -323,7 +334,7 @@
continuously change this state by issuing some commands---for example
for updating a field in an array or for adding one to a variable
stored in memory and so on. The classic example for this style of
-programming is a \texttt{for}-loop in C/C++. Consider the snippet:
+programming is a \texttt{for}-loop in say Java and C/C++. Consider the snippet:
\begin{lstlisting}[language=C,numbers=none]
for (int i = 10; i < 20; i++) {
@@ -358,33 +369,42 @@
upon the fact that approximately every 2 years their code would run
twice as fast because the clock-speed of their CPUs got twice as fast.
-Unfortunately this does not happen any more nowadays. To get you out of
-this dreadful situation, CPU producers pile more and more cores into
-CPUs in order to make them more powerful and potentially make software
-faster. The task for you as developer is to take somehow advantage of
-these cores by running as much of your code as possible in parallel on
-as many cores you have available (typically 4-8 or even more in modern laptops
-and sometimes much more on high-end machines). In this situation
-\textit{mutable} variables like \texttt{i} in the C-code above are evil,
-or at least a major nuisance: Because if you want to distribute some of
-the loop-iterations over several cores that are currently idle in your
-system, you need to be extremely careful about who can read and
-overwrite the variable \texttt{i}.\footnote{If you are of the mistaken
-belief that nothing nasty can happen to \texttt{i} inside the
-\texttt{for}-loop, then you need to go back over the C++ material.}
+Unfortunately this does not happen any more nowadays. To get you out
+of this dreadful situation, CPU producers pile more and more cores
+into CPUs in order to make them more powerful and potentially make
+software faster. The task for you as developer is to take somehow
+advantage of these cores by running as much of your code as possible
+in parallel on as many cores you have available (typically 4-8 or even
+more in modern laptops and sometimes much more on high-end
+machines---and we conveniently ignore how many cores are on modern
+GPUs). In this situation \textit{mutable} variables like \texttt{i} in
+the for-loop above are evil, or at least a major nuisance: Because if
+you want to distribute some of the loop-iterations over several cores
+that are currently idle in your system, you need to be extremely
+careful about who can read and overwrite the variable
+\texttt{i}.\footnote{If you are of the mistaken belief that nothing
+ nasty can happen to \texttt{i} inside the \texttt{for}-loop, then
+ you will have to be extra careful with the C++ material.}
Especially the writing operation is critical because you do not want
that conflicting writes mess about with \texttt{i}. Take my word: an
-untold amount of misery has arisen from this problem. The catch is that
-if you try to solve this problem in C/C++ or Java, and be as defensive
-as possible about reads and writes to \texttt{i}, then you need to
-synchronise access to it. The result is that very often your program
-waits more than it runs, thereby defeating the point of trying to run
-the program in parallel in the first place. If you are less defensive,
-then usually all hell breaks loose by seemingly obtaining random
-results. And forget the idea of being able to debug such code.
+untold amount of misery has arisen from this problem. The catch is
+that if you try to solve this problem in C/C++ or Java, and be as
+defensive as possible about reads and writes to \texttt{i}, then you
+need to synchronise access to it. The result is that very often your
+program waits more than it runs, thereby defeating the point of trying
+to run the program in parallel in the first place. If you are less
+defensive, then usually all hell breaks loose by seemingly obtaining
+random results. And forget the idea of being able to debug such
+code. If you want to watch a 5-minute video of horror stories, feel
+free to follow \ldots{}
+\hr{https://www.youtube.com/watch?v=LdLUgCJkiHY} (I love the fact, he
+says at 4:02 that he does not understand how the JVM really
+works\ldots{} I always assumed I am the only idiot who does not
+understand how threads work on the JVM. Apparently not. \raisebox{-0.7mm}{\emoji{rofl}})\bigskip
+\noindent
The central idea of functional programming is to eliminate any state
-from programs---or at least from the ``interesting bits'' of the
+and mutable variables 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
@@ -456,14 +476,14 @@
\centering\includegraphics[scale=0.5]{../pics/cpu1.png}
\end{tabular}
\end{center}
-\caption{The code of the ``main'' loops in my version of the mandelbrot program.
+\caption{The code of the two ``main'' loops in my version of the mandelbrot program.
The parallel version differs only in \texttt{.par} being added to the
``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 easy \emph{parallelisation}
-only works reliably with an immutable program.
+only works reliably with immutable programs.
\label{mand}}
\end{boxedminipage}
\end{figure}
@@ -519,7 +539,7 @@
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
$ scala-cli
-Welcome to Scala 3.3.1 (17.0.8.1, Java OpenJDK 64-Bit Server VM).
+Welcome to Scala 3.4.1 (21.0.2, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala>
@@ -656,7 +676,7 @@
In the lectures I will try to avoid as much as possible the term
\emph{variables} familiar from other programming languages. The reason
is that Scala has \emph{values}, which can be seen as abbreviations of
-larger expressions. The keyword for defining values is \code{val}.
+potentially larger expressions. The keyword for defining values is \code{val}.
For example
\begin{lstlisting}[numbers=none]
@@ -724,7 +744,7 @@
\code{String}, the result of this function will be of type
\code{String}. It is a good habit to always include this information
about the return type, while it is only strictly necessary to give this
-type in recursive functions. Simple examples of Scala functions are:
+type in recursive functions (later more on that). Simple examples of Scala functions are:
\begin{lstlisting}[numbers=none]
def incr(x: Int) : Int = x + 1
@@ -733,7 +753,7 @@
\end{lstlisting}
\noindent
-The general scheme for a function is
+The general scheme for functions is
\begin{lstlisting}[numbers=none]
def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
@@ -771,7 +791,7 @@
Notice that I did not use a \code{then}-keyword in the
\code{if}-statements and that I enclosed the condition inside
parentheses, like \code{(n == 0)}. Your eyes might hurt to not see an
-\code{else} with an \code{if}, but this has been long established
+\code{then} with an \code{if}, but this has been long established
syntax for \code{if}-statements. Scala, to my knowledge, was pretty
unique in that for nearly 20 years of its existence\ldots{}until Scala
3 came along. While people like me have perfectly adapted to the sight
@@ -781,7 +801,8 @@
\begin{lstlisting}[numbers=none]
def fact(n: Int) : Int = {
- if n == 0 then 1
+ if n == 0
+ then 1
else n * fact(n - 1)
}
\end{lstlisting}
@@ -795,7 +816,8 @@
\begin{lstlisting}[numbers=none]
def fact(n: Int) : Int =
- if n == 0 then 1
+ if n == 0
+ then 1
else n * fact(n - 1)
\end{lstlisting}
@@ -805,12 +827,12 @@
I will \textbf{not} show you any of my code in the newfangled
Pythonesque meaningful-indent-syntax. When necessary, I will always
use braces to indicate the beginning and end of a code block, and I
-have not yet get used to the \code{if}s with
-\code{then}s.\footnote{Scala adopted some very fine features of Python, for example string interpolations, but that we had to completely cave in to
- the demands of Gen-Python is a step to far for my completely
- insignificant opinion. For me this is a bridge too far.
+have not yet get completely get used to the \code{if}s with
+\code{then}s. Please forgive me for being still inconsistent with this\footnote{Scala adopted some very fine features of Python, for example string interpolations, but that we had to completely cave in to
+ the demands of Gen-Python is a bridge too far for my completely
+ insignificant opinion.
I always assumed escaping Python's dependency hell
-is every software developers life goal---apparently not. ;o)}
+is every software developers life goal---apparently not. \emoji{exploding-head}}
However, no matter which syntax style you adopt for \code{if}s, never