137 \mbox{}\hfill\textit{ --- a joke(?) found on Twitter}\bigskip |
137 \mbox{}\hfill\textit{ --- a joke(?) found on Twitter}\bigskip |
138 |
138 |
139 \mbox{}\hfill\textit{``Life is too short for \texttt{malloc}.''}\smallskip\\ |
139 \mbox{}\hfill\textit{``Life is too short for \texttt{malloc}.''}\smallskip\\ |
140 \mbox{}\hfill\textit{ --- said Neal Ford at Oscon'13}\;\hr{https://www.youtube.com/watch?v=7aYS9PcAITQ}\bigskip\\ |
140 \mbox{}\hfill\textit{ --- said Neal Ford at Oscon'13}\;\hr{https://www.youtube.com/watch?v=7aYS9PcAITQ}\bigskip\\ |
141 |
141 |
|
142 % In 1982, James or Jim Morris wrote: |
|
143 % |
|
144 % Functional languages are unnatural to use; but so are knives and |
|
145 % forks, diplomatic protocols, double-entry bookkeeping, and a host of |
|
146 % other things modern civilization has found useful. |
|
147 % |
|
148 % Real Programming in Functional Languages |
|
149 % Xerox PARC technical report |
|
150 % CSL·81·11 July 1,1981 |
|
151 |
|
152 |
142 |
153 |
143 \subsection*{Introduction} |
154 \subsection*{Introduction} |
144 |
155 |
145 \noindent |
156 \noindent |
146 Scala is a programming language that combines functional and |
157 Scala is a programming language that combines functional and |
147 object-oriented programming-styles. It has received quite a bit of |
158 object-oriented programming-styles. It has received quite a bit of |
148 attention in the last five or so years. One reason for this attention is |
159 attention in the last ten or so years. One reason for this attention is |
149 that, like the Java programming language, Scala compiles to the Java |
160 that, like the Java programming language, Scala compiles to the Java |
150 Virtual Machine (JVM) and therefore Scala programs can run under MacOSX, |
161 Virtual Machine (JVM) and therefore Scala programs can run under MacOSX, |
151 Linux and Windows. Because of this it has also access to |
162 Linux and Windows. Because of this it has also access to |
152 the myriads of Java libraries. Unlike Java, however, Scala often allows |
163 the myriads of Java libraries. Unlike Java, however, Scala often allows |
153 programmers to write very concise and elegant code. Some therefore say |
164 programmers to write very concise and elegant code. Some therefore say |
154 ``Scala is the better Java''.\footnote{from |
165 ``Scala is the better Java''.\footnote{from |
155 \url{https://www.slideshare.net/maximnovak/joy-of-scala}, though this might |
166 \url{https://www.slideshare.net/maximnovak/joy-of-scala}, though this might |
156 be outdated as latest versions of Java are catching up somewhat} |
167 be outdated as latest versions of Java are catching up somewhat} |
157 |
168 |
158 A number of companies---the Guardian, Dualingo, Coursera, FourSquare, |
169 A number of companies---the Guardian, Duolingo, Coursera, FourSquare, |
159 Netflix, LinkedIn, ITV to name a few---either use Scala exclusively in |
170 Netflix, LinkedIn, ITV to name a few---either use Scala exclusively in |
160 production code, or at least to some substantial degree. Scala seems |
171 production code, or at least to some substantial degree. Scala seems |
161 also useful in job-interviews (especially in data science) according to |
172 also useful in job-interviews (especially in data science) according to |
162 this anecdotal report |
173 this anecdotal report |
163 |
174 |
171 \begin{quote} |
182 \begin{quote} |
172 \url{http://www.scala-lang.org}\medskip |
183 \url{http://www.scala-lang.org}\medskip |
173 \end{quote} |
184 \end{quote} |
174 |
185 |
175 \noindent\alert |
186 \noindent\alert |
176 Just make sure you are using the version 3(!) of Scala. This is |
187 For PEP, make sure you are using the version 3(!) of Scala. This is |
177 the version I am going to use in the lectures and in the coursework. This |
188 the version I am going to use in the lectures and in the coursework. This |
178 can be any version of Scala 3.X where $X=\{1,2,3\}$. Also the minor |
189 can be any version of Scala 3.X where $X=\{3,4\}$. Also the minor |
179 number does not matter. Note that this will be the first year I am |
190 number does not matter. Note that this will be the second year I am |
180 using this newer version -- so some hiccups are bound to happen. Apologies |
191 using this newer version of Scala -- some hiccups can still happen. Apologies |
181 in advance!\bigskip |
192 in advance!\bigskip |
182 |
193 |
183 \begin{tcolorbox}[colback=red!5!white,colframe=red!75!black] |
194 \begin{tcolorbox}[colback=red!5!white,colframe=red!75!black] |
184 I will be using the \textbf{\texttt{scala-cli}} REPL for Scala 3, rather |
195 I will be using the \textbf{\texttt{scala-cli}} REPL for Scala 3, rather |
185 than the ``plain'' Scala REPL. This is a batteries included version of |
196 than the ``plain'' Scala REPL. This is a batteries included version of |
212 obviously Windows.\footnote{\ldots{}unlike \emph{Microsoft Visual Studio}---note |
223 obviously Windows.\footnote{\ldots{}unlike \emph{Microsoft Visual Studio}---note |
213 the minuscule difference in the name---which is a heavy-duty, |
224 the minuscule difference in the name---which is a heavy-duty, |
214 Windows-only IDE\ldots{}jeez, with all their money could they not have come |
225 Windows-only IDE\ldots{}jeez, with all their money could they not have come |
215 up with a completely different name for a complete different project? |
226 up with a completely different name for a complete different project? |
216 For the pedantic, Microsoft Visual Studio is an IDE, whereas Visual |
227 For the pedantic, Microsoft Visual Studio is an IDE, whereas Visual |
217 Studio Code is considered to be a \emph{source code editor}. Anybody knows what the |
228 Studio Code is considered to be a \emph{source code editor}. Anybody |
|
229 out there knows what the |
218 difference is?} It can be downloaded for free from |
230 difference is?} It can be downloaded for free from |
219 |
231 |
220 \begin{quote} |
232 \begin{quote} |
221 \url{https://code.visualstudio.com} |
233 \url{https://code.visualstudio.com} |
222 \end{quote} |
234 \end{quote} |
257 |
269 |
258 |
270 |
259 What I like most about VS Code/Codium is that it provides easy access |
271 What I like most about VS Code/Codium is that it provides easy access |
260 to any Scala REPL. But if you prefer another editor for coding, it is |
272 to any Scala REPL. But if you prefer another editor for coding, it is |
261 also painless to work with Scala completely on the command line (as |
273 also painless to work with Scala completely on the command line (as |
262 you might have done with \texttt{g++} in the earlier part of PEP). For |
274 you might do with \texttt{g++} in the second part of PEP). For |
263 the lazybones among us, there are even online editors and environments |
275 the lazybones among us, there are even online editors and environments |
264 for developing and running Scala programs: \textit{Scastie} and |
276 for developing and running Scala programs: for example \textit{Scastie} |
265 \textit{ScalaFiddle} are two of them. They require zero setup |
277 is one of them. It requires zero setup |
266 (assuming you have a browser handy). You can access them at |
278 (assuming you have a browser handy). You can access it at |
267 |
279 |
268 \begin{quote} |
280 \begin{quote} |
269 \url{https://scastie.scala-lang.org}\\ |
281 \url{https://scastie.scala-lang.org} |
270 \url{https://scalafiddle.io}\medskip |
|
271 \end{quote} |
282 \end{quote} |
272 |
283 |
273 \noindent |
284 \noindent |
274 But you should be careful if you use them for your coursework: they |
285 But you should be careful if you use them for your coursework: they |
275 are meant to play around, not really for serious work. Make |
286 are meant to play around, not really for serious work. Make |
296 |
307 |
297 \subsection*{Why Functional Programming?} |
308 \subsection*{Why Functional Programming?} |
298 |
309 |
299 Before we go on, let me explain a bit more why we want to inflict upon |
310 Before we go on, let me explain a bit more why we want to inflict upon |
300 you another programming language. You hopefully have mastered Java and |
311 you another programming language. You hopefully have mastered Java and |
301 C++, possibly Python\ldots{} the world should be your oyster, no? |
312 soon will master C++ as well, you possibly know Python already\ldots{} |
302 Well, matters are not as simple as one might wish. We do require Scala |
313 the world should be your oyster, no? Well, as usual matters are not as simple |
303 in PEP, but actually we do not religiously care whether you learn |
314 as one might wish. We do require Scala in PEP, but actually we do not |
304 Scala---after all it is just a programming language (albeit a nifty |
315 religiously care whether you learn Scala---after all it is just a |
305 one IMHO). What we do care about is that you learn about |
316 programming language (albeit a nifty one IMHO). What we do care about |
306 \textit{functional programming}. Scala is just the vehicle for |
317 is that you learn about \textit{functional programming}. Scala is just |
307 that. Still, you need to learn Scala well enough to get good marks in |
318 the vehicle for that. Still, you need to learn Scala well enough to |
308 PEP, but functional programming could perhaps equally be taught with |
319 get good marks in PEP, but functional programming could perhaps |
309 Haskell, F\#, SML, Ocaml, Kotlin, Clojure, Scheme, Elm and many other |
320 equally be taught with Haskell, F\#, SML, Ocaml, Kotlin, Clojure, |
310 functional programming languages. |
321 Scheme, Elm and many other functional programming languages. |
311 |
322 |
312 %Your friendly lecturer just |
323 %Your friendly lecturer just |
313 %happens to like Scala and the Department agreed that it is a good idea |
324 %happens to like Scala and the Department agreed that it is a good idea |
314 %to inflict Scala upon you. |
325 %to inflict Scala upon you. |
315 |
326 |
321 C/C++). The main idea of imperative programming is that |
332 C/C++). The main idea of imperative programming is that |
322 you have some form of \emph{state} in your program and you |
333 you have some form of \emph{state} in your program and you |
323 continuously change this state by issuing some commands---for example |
334 continuously change this state by issuing some commands---for example |
324 for updating a field in an array or for adding one to a variable |
335 for updating a field in an array or for adding one to a variable |
325 stored in memory and so on. The classic example for this style of |
336 stored in memory and so on. The classic example for this style of |
326 programming is a \texttt{for}-loop in C/C++. Consider the snippet: |
337 programming is a \texttt{for}-loop in say Java and C/C++. Consider the snippet: |
327 |
338 |
328 \begin{lstlisting}[language=C,numbers=none] |
339 \begin{lstlisting}[language=C,numbers=none] |
329 for (int i = 10; i < 20; i++) { |
340 for (int i = 10; i < 20; i++) { |
330 //...do something with i... |
341 //...do something with i... |
331 } |
342 } |
356 no dramatic increases are predicted for any time soon. So you are a bit |
367 no dramatic increases are predicted for any time soon. So you are a bit |
357 stuck. This is unlike previous generations of developers who could rely |
368 stuck. This is unlike previous generations of developers who could rely |
358 upon the fact that approximately every 2 years their code would run |
369 upon the fact that approximately every 2 years their code would run |
359 twice as fast because the clock-speed of their CPUs got twice as fast. |
370 twice as fast because the clock-speed of their CPUs got twice as fast. |
360 |
371 |
361 Unfortunately this does not happen any more nowadays. To get you out of |
372 Unfortunately this does not happen any more nowadays. To get you out |
362 this dreadful situation, CPU producers pile more and more cores into |
373 of this dreadful situation, CPU producers pile more and more cores |
363 CPUs in order to make them more powerful and potentially make software |
374 into CPUs in order to make them more powerful and potentially make |
364 faster. The task for you as developer is to take somehow advantage of |
375 software faster. The task for you as developer is to take somehow |
365 these cores by running as much of your code as possible in parallel on |
376 advantage of these cores by running as much of your code as possible |
366 as many cores you have available (typically 4-8 or even more in modern laptops |
377 in parallel on as many cores you have available (typically 4-8 or even |
367 and sometimes much more on high-end machines). In this situation |
378 more in modern laptops and sometimes much more on high-end |
368 \textit{mutable} variables like \texttt{i} in the C-code above are evil, |
379 machines---and we conveniently ignore how many cores are on modern |
369 or at least a major nuisance: Because if you want to distribute some of |
380 GPUs). In this situation \textit{mutable} variables like \texttt{i} in |
370 the loop-iterations over several cores that are currently idle in your |
381 the for-loop above are evil, or at least a major nuisance: Because if |
371 system, you need to be extremely careful about who can read and |
382 you want to distribute some of the loop-iterations over several cores |
372 overwrite the variable \texttt{i}.\footnote{If you are of the mistaken |
383 that are currently idle in your system, you need to be extremely |
373 belief that nothing nasty can happen to \texttt{i} inside the |
384 careful about who can read and overwrite the variable |
374 \texttt{for}-loop, then you need to go back over the C++ material.} |
385 \texttt{i}.\footnote{If you are of the mistaken belief that nothing |
|
386 nasty can happen to \texttt{i} inside the \texttt{for}-loop, then |
|
387 you will have to be extra careful with the C++ material.} |
375 Especially the writing operation is critical because you do not want |
388 Especially the writing operation is critical because you do not want |
376 that conflicting writes mess about with \texttt{i}. Take my word: an |
389 that conflicting writes mess about with \texttt{i}. Take my word: an |
377 untold amount of misery has arisen from this problem. The catch is that |
390 untold amount of misery has arisen from this problem. The catch is |
378 if you try to solve this problem in C/C++ or Java, and be as defensive |
391 that if you try to solve this problem in C/C++ or Java, and be as |
379 as possible about reads and writes to \texttt{i}, then you need to |
392 defensive as possible about reads and writes to \texttt{i}, then you |
380 synchronise access to it. The result is that very often your program |
393 need to synchronise access to it. The result is that very often your |
381 waits more than it runs, thereby defeating the point of trying to run |
394 program waits more than it runs, thereby defeating the point of trying |
382 the program in parallel in the first place. If you are less defensive, |
395 to run the program in parallel in the first place. If you are less |
383 then usually all hell breaks loose by seemingly obtaining random |
396 defensive, then usually all hell breaks loose by seemingly obtaining |
384 results. And forget the idea of being able to debug such code. |
397 random results. And forget the idea of being able to debug such |
385 |
398 code. If you want to watch a 5-minute video of horror stories, feel |
|
399 free to follow \ldots{} |
|
400 \hr{https://www.youtube.com/watch?v=LdLUgCJkiHY} (I love the fact, he |
|
401 says at 4:02 that he does not understand how the JVM really |
|
402 works\ldots{} I always assumed I am the only idiot who does not |
|
403 understand how threads work on the JVM. Apparently not. \raisebox{-0.7mm}{\emoji{rofl}})\bigskip |
|
404 |
|
405 \noindent |
386 The central idea of functional programming is to eliminate any state |
406 The central idea of functional programming is to eliminate any state |
387 from programs---or at least from the ``interesting bits'' of the |
407 and mutable variables from programs---or at least from the ``interesting bits'' of the |
388 programs. Because then it is easy to parallelise the resulting |
408 programs. Because then it is easy to parallelise the resulting |
389 programs: if you do not have any state, then once created, all memory |
409 programs: if you do not have any state, then once created, all memory |
390 content stays unchanged and reads to such memory are absolutely safe |
410 content stays unchanged and reads to such memory are absolutely safe |
391 without the need of any synchronisation. An example is given in |
411 without the need of any synchronisation. An example is given in |
392 Figure~\ref{mand} where in the absence of the annoying state, Scala |
412 Figure~\ref{mand} where in the absence of the annoying state, Scala |
454 |
474 |
455 \centering\includegraphics[scale=0.5]{../pics/cpu2.png} & |
475 \centering\includegraphics[scale=0.5]{../pics/cpu2.png} & |
456 \centering\includegraphics[scale=0.5]{../pics/cpu1.png} |
476 \centering\includegraphics[scale=0.5]{../pics/cpu1.png} |
457 \end{tabular} |
477 \end{tabular} |
458 \end{center} |
478 \end{center} |
459 \caption{The code of the ``main'' loops in my version of the mandelbrot program. |
479 \caption{The code of the two ``main'' loops in my version of the mandelbrot program. |
460 The parallel version differs only in \texttt{.par} being added to the |
480 The parallel version differs only in \texttt{.par} being added to the |
461 ``ranges'' of the x and y coordinates. As can be seen from the CPU loads, in |
481 ``ranges'' of the x and y coordinates. As can be seen from the CPU loads, in |
462 the sequential version there is a lower peak for an extended period, |
482 the sequential version there is a lower peak for an extended period, |
463 while in the parallel version there is a short sharp burst for |
483 while in the parallel version there is a short sharp burst for |
464 essentially the same workload\ldots{}meaning you get more work done |
484 essentially the same workload\ldots{}meaning you get more work done |
465 in a shorter amount of time. This easy \emph{parallelisation} |
485 in a shorter amount of time. This easy \emph{parallelisation} |
466 only works reliably with an immutable program. |
486 only works reliably with immutable programs. |
467 \label{mand}} |
487 \label{mand}} |
468 \end{boxedminipage} |
488 \end{boxedminipage} |
469 \end{figure} |
489 \end{figure} |
470 |
490 |
471 But remember this easy parallelisation of code requires that we have no |
491 But remember this easy parallelisation of code requires that we have no |
722 This function returns the value resulting from evaluating the expression |
742 This function returns the value resulting from evaluating the expression |
723 \code{EXPR} (whatever is substituted for this). Since we declared |
743 \code{EXPR} (whatever is substituted for this). Since we declared |
724 \code{String}, the result of this function will be of type |
744 \code{String}, the result of this function will be of type |
725 \code{String}. It is a good habit to always include this information |
745 \code{String}. It is a good habit to always include this information |
726 about the return type, while it is only strictly necessary to give this |
746 about the return type, while it is only strictly necessary to give this |
727 type in recursive functions. Simple examples of Scala functions are: |
747 type in recursive functions (later more on that). Simple examples of Scala functions are: |
728 |
748 |
729 \begin{lstlisting}[numbers=none] |
749 \begin{lstlisting}[numbers=none] |
730 def incr(x: Int) : Int = x + 1 |
750 def incr(x: Int) : Int = x + 1 |
731 def double(x: Int) : Int = x + x |
751 def double(x: Int) : Int = x + x |
732 def square(x: Int) : Int = x * x |
752 def square(x: Int) : Int = x * x |
733 \end{lstlisting} |
753 \end{lstlisting} |
734 |
754 |
735 \noindent |
755 \noindent |
736 The general scheme for a function is |
756 The general scheme for functions is |
737 |
757 |
738 \begin{lstlisting}[numbers=none] |
758 \begin{lstlisting}[numbers=none] |
739 def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = { |
759 def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = { |
740 ...BODY... |
760 ...BODY... |
741 } |
761 } |
769 \noindent |
789 \noindent |
770 but this seems a bit overkill for a small function like \code{fact}. |
790 but this seems a bit overkill for a small function like \code{fact}. |
771 Notice that I did not use a \code{then}-keyword in the |
791 Notice that I did not use a \code{then}-keyword in the |
772 \code{if}-statements and that I enclosed the condition inside |
792 \code{if}-statements and that I enclosed the condition inside |
773 parentheses, like \code{(n == 0)}. Your eyes might hurt to not see an |
793 parentheses, like \code{(n == 0)}. Your eyes might hurt to not see an |
774 \code{else} with an \code{if}, but this has been long established |
794 \code{then} with an \code{if}, but this has been long established |
775 syntax for \code{if}-statements. Scala, to my knowledge, was pretty |
795 syntax for \code{if}-statements. Scala, to my knowledge, was pretty |
776 unique in that for nearly 20 years of its existence\ldots{}until Scala |
796 unique in that for nearly 20 years of its existence\ldots{}until Scala |
777 3 came along. While people like me have perfectly adapted to the sight |
797 3 came along. While people like me have perfectly adapted to the sight |
778 of \code{if}s without \code{then}s, it seems the developers of Scala |
798 of \code{if}s without \code{then}s, it seems the developers of Scala |
779 caved in to the special eyesight of Gen-Python people and now allow to |
799 caved in to the special eyesight of Gen-Python people and now allow to |
780 write the same function also as |
800 write the same function also as |
781 |
801 |
782 \begin{lstlisting}[numbers=none] |
802 \begin{lstlisting}[numbers=none] |
783 def fact(n: Int) : Int = { |
803 def fact(n: Int) : Int = { |
784 if n == 0 then 1 |
804 if n == 0 |
|
805 then 1 |
785 else n * fact(n - 1) |
806 else n * fact(n - 1) |
786 } |
807 } |
787 \end{lstlisting} |
808 \end{lstlisting} |
788 |
809 |
789 \noindent |
810 \noindent |
793 completely beyond me. So in Scala 3 the braces are optional and the |
814 completely beyond me. So in Scala 3 the braces are optional and the |
794 \texttt{fact}-function can even be written as |
815 \texttt{fact}-function can even be written as |
795 |
816 |
796 \begin{lstlisting}[numbers=none] |
817 \begin{lstlisting}[numbers=none] |
797 def fact(n: Int) : Int = |
818 def fact(n: Int) : Int = |
798 if n == 0 then 1 |
819 if n == 0 |
|
820 then 1 |
799 else n * fact(n - 1) |
821 else n * fact(n - 1) |
800 \end{lstlisting} |
822 \end{lstlisting} |
801 |
823 |
802 \noindent on the condition that indents now become \emph{meaningful} |
824 \noindent on the condition that indents now become \emph{meaningful} |
803 (as in Python).\raisebox{-0.7mm}{\emoji{face-vomiting}} All versions are now permitted in Scala 3. While you |
825 (as in Python).\raisebox{-0.7mm}{\emoji{face-vomiting}} All versions are now permitted in Scala 3. While you |
804 are free to use any syntax version you want in PEP, or even mix them, |
826 are free to use any syntax version you want in PEP, or even mix them, |
805 I will \textbf{not} show you any of my code in the newfangled |
827 I will \textbf{not} show you any of my code in the newfangled |
806 Pythonesque meaningful-indent-syntax. When necessary, I will always |
828 Pythonesque meaningful-indent-syntax. When necessary, I will always |
807 use braces to indicate the beginning and end of a code block, and I |
829 use braces to indicate the beginning and end of a code block, and I |
808 have not yet get used to the \code{if}s with |
830 have not yet get completely get used to the \code{if}s with |
809 \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 |
831 \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 |
810 the demands of Gen-Python is a step to far for my completely |
832 the demands of Gen-Python is a bridge too far for my completely |
811 insignificant opinion. For me this is a bridge too far. |
833 insignificant opinion. |
812 I always assumed escaping Python's dependency hell |
834 I always assumed escaping Python's dependency hell |
813 is every software developers life goal---apparently not. ;o)} |
835 is every software developers life goal---apparently not. \emoji{exploding-head}} |
814 |
836 |
815 |
837 |
816 However, no matter which syntax style you adopt for \code{if}s, never |
838 However, no matter which syntax style you adopt for \code{if}s, never |
817 write an \code{if} without an \code{else}-branch! That has something |
839 write an \code{if} without an \code{else}-branch! That has something |
818 to do with functional programming and you will see its purpose later |
840 to do with functional programming and you will see its purpose later |