# HG changeset patch # User Christian Urban # Date 1572828670 0 # Node ID b192bc77261372afc8cf9f601bd02f759e894edf # Parent e86add5a69618a8a4d3663c7f34a36745720635f updated diff -r e86add5a6961 -r b192bc772613 pics/pure.png Binary file pics/pure.png has changed diff -r e86add5a6961 -r b192bc772613 pics/salary1.png Binary file pics/salary1.png has changed diff -r e86add5a6961 -r b192bc772613 pics/salary2.png Binary file pics/salary2.png has changed diff -r e86add5a6961 -r b192bc772613 progs/lecture2.scala --- a/progs/lecture2.scala Sun Nov 03 14:42:17 2019 +0000 +++ b/progs/lecture2.scala Mon Nov 04 00:51:10 2019 +0000 @@ -525,6 +525,45 @@ } +// Recursion +//=========== + +/* a, b, c + +aa aaa +ab baa +ac caa +ba => ...... +bb +bc +ca +cb +cc + +*/ + +def perms(cs: List[Char], l: Int) : List[String] = { + if (l == 0) List("") + else for (c <- cs; s <- perms(cs, l - 1)) yield s"$c$s" +} + +perms("abc".toList, 2) + +def move(from: Char, to: Char) = + println(s"Move disc from $from to $to!") + +def hanoi(n: Int, from: Char, via: Char, to: Char) : Unit = { + if (n == 0) () + else { + hanoi(n - 1, from, to, via) + move(from, to) + hanoi(n - 1, via, from, to) + } +} + +hanoi(40, 'A', 'B', 'C') + + // Tail Recursion //================ diff -r e86add5a6961 -r b192bc772613 progs/mandelbrot.scala --- a/progs/mandelbrot.scala Sun Nov 03 14:42:17 2019 +0000 +++ b/progs/mandelbrot.scala Mon Nov 04 00:51:10 2019 +0000 @@ -1,9 +1,11 @@ // Mandelbrot pictures +//===================== // // see https://en.wikipedia.org/wiki/Mandelbrot_set // -// under scala 2.13 needs to be called with -// scala -cp `coursier fetch -p org.scala-lang.modules:scala-parallel-collections_2.13:0.2.0` mandelbrot.scala +// under scala 2.13.1 needs to be called with +// +// scala -cp scala-parallel-collections_2.13-0.2.0.jar mandelbrot.scala import java.awt.Color import java.awt.Dimension @@ -102,8 +104,8 @@ val d_x = (end.re - start.re) / W val d_y = (end.im - start.im) / H - for (y <- (0 until H).par) { - for (x <- (0 until W).par) { + for (y <- (0 until H)) { + for (x <- (0 until W)) { val c = start + (x * d_x + y * d_y * i) @@ -135,7 +137,7 @@ val exa1 = -2.0 + -1.5 * i val exa2 = 1.0 + 1.5 * i -time_needed(mandelbrot(exa1, exa2, 1000)) +println(s"${time_needed(mandelbrot(exa1, exa2, 1000))} secs") // example 2 val exb1 = -0.37465401 + 0.659227668 * i @@ -156,25 +158,19 @@ val delta = (exc2 - exc1) * 0.0333 /* -time_needed( +println(s"${time_needed( for (n <- (0 to 12)) mandelbrot(exc1 + delta * n, - exc2 - delta * n, 100)) + exc2 - delta * n, 100))} secs") */ -/* -time_needed( - for (n <- (0 to 12)) - mandelbrot(exc1 + delta * n, - exc2 - delta * n, 1000)) -*/ // Larry Paulson's example val exl1 = -0.74364990 + 0.13188170 * i val exl2 = -0.74291189 + 0.13261971 * i -//time_needed(mandelbrot(exl1, exl2, 1000)) +//println(s"${time_needed(mandelbrot(exl1, exl2, 1000))} secs") // example by Jorgen Villadsen diff -r e86add5a6961 -r b192bc772613 slides/slides01.pdf Binary file slides/slides01.pdf has changed diff -r e86add5a6961 -r b192bc772613 slides/slides01.tex --- a/slides/slides01.tex Sun Nov 03 14:42:17 2019 +0000 +++ b/slides/slides01.tex Mon Nov 04 00:51:10 2019 +0000 @@ -4,6 +4,7 @@ \usepackage{../graphics} \usepackage{../langs} %\usepackage{../data} +\usetikzlibrary{shapes} \hfuzz=220pt @@ -16,7 +17,29 @@ numbers=none, xleftmargin=0mm} -\newcommand{\bl}[1]{\textcolor{blue}{#1}} +\newcommand{\bl}[1]{\textcolor{blue}{#1}} + +\newcommand{\RIGHTarrow}[3]{% +\begin{textblock}{0}(#2,#3)% +\onslide<#1>{% +\begin{tikzpicture}% +\node at (0,0) [single arrow, shape border rotate=0, fill=red,text=red]{a};% +\end{tikzpicture}}% +\end{textblock}} +\newcommand{\LEFTarrow}[3]{% +\begin{textblock}{0}(#2,#3)% +\onslide<#1>{% +\begin{tikzpicture}% +\node at (0,0) [single arrow, shape border rotate=180, fill=red,text=red]{a};% +\end{tikzpicture}}% +\end{textblock}} +\newcommand{\DOWNarrow}[3]{% +\begin{textblock}{0}(#2,#3)% +\onslide<#1>{% +\begin{tikzpicture}% +\node at (0,0) [single arrow, shape border rotate=270, fill=red,text=red]{a};% +\end{tikzpicture}}% +\end{textblock}} % beamer stuff \renewcommand{\slidecaption}{PEP (Scala) \liningnums{01}, King's College London} @@ -37,8 +60,8 @@ \begin{center} \begin{tabular}{ll} Email: & christian.urban at kcl.ac.uk\\ - Office: & N\liningnums{7.07} (North Wing, Bush House)\\ - Slides \& Code: & KEATS\medskip\\ + Office: & N\liningnums{7.07} (North Wing, Bush House)\bigskip\\ + Slides \& Code: & KEATS\bigskip\\ Office Hours: & Thursdays 12:00 -- 14:00\\ Additionally: & (for Scala) Tuesdays 10:45 -- 11:45\\ \end{tabular} @@ -94,18 +117,6 @@ \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%\begin{frame}[c] -%\frametitle{Why Scala?} -% -%In the last few years there is a ``Cambrian explosion'' of -%languages from both academia and industry. -% -%It is essential for students to have skills to pick up new languages -%quickly. -% -%\end{frame} -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -153,16 +164,6 @@ \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%\begin{frame}[c] -%\frametitle{Scala (imperative) vs Scala (functional)} -% -%Reham's example -% -% -%\end{frame} -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -184,19 +185,50 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\begin{frame}[t] +\frametitle{Why Scala?} + +\onslide<2->{% +\begin{center} +{\large\bf{}Money?}\bigskip\\ +\begin{tabular}{@{}c@{}c@{}} +\includegraphics[scale=0.2]{../pics/salary1.png} & +\includegraphics[scale=0.2]{../pics/salary2.png}\\ +\multicolumn{2}{r@{}}{\footnotesize$^*$ source: Stackoverflow Developer Survey, 2019} +\end{tabular} +\end{center} +\RIGHTarrow{2}{2.3}{5.45} +\DOWNarrow{2}{10.1}{3.6} +} + + +\small +Elm, Rust, Haskell, Ocaml, F$\#$, Erlang, ML, Lisp (Racket)\ldots + +\only<3>{ +\begin{textblock}{6}(3.3,6.2) +\begin{bubble}[6.5cm] +\bf\huge\textcolor{RoyalBlue}{Functional Programming!} +\end{bubble} +\end{textblock}} + +\end{frame} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[c] - \frametitle{\bf - \only<1>{\begin{tabular}{l}\\[2mm]Why Scala?\\ \mbox{}\end{tabular}} - \only<2->{\begin{tabular}{c}\\[2mm]Why Functional\\[-2mm] Programming?\end{tabular}} + \frametitle{\begin{tabular}{c}\\[2mm] + Why Functional\\[-2mm] Programming?\end{tabular} } % \mbox{}\\[3.5cm] \small -Scala, Elm, Haskell, Ocaml, F$\sharp$, Erlang, ML, Lisp (Racket), \ldots +Elm, Haskell, Ocaml, F$\#$, Erlang, ML, Lisp (Racket)\ldots -\only<3>{ -\begin{textblock}{6}(2,6.2) +\only<2>{ +\begin{textblock}{6}(1.3,6.2) \begin{bubble}[10cm] \normalsize``If you want to see which features will be in mainstream programming languages tomorrow, then take a look at functional programming @@ -206,10 +238,10 @@ \end{bubble} \end{textblock}} -\only<4>{ +\only<3>{ \begin{textblock}{6}(5,5.8) \hspace{2mm}\includegraphics[scale=0.06]{../pics/assign.jpg}\\[-1mm] -\Large\alert{\textbf{Immutability}} +\Large\alert{\hspace{5mm}\textbf{Immutability}} \end{textblock}} @@ -217,6 +249,20 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\begin{frame}[c] + +\begin{center} +\includegraphics[scale=0.4]{../pics/pure.png} +\end{center} + +\begin{textblock}{6}(9,14) +\tiny$^*$ from ``What pure functional programming is all about?'' +\end{textblock} +\end{frame} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[c, fragile] {\Large Why bother? or\smallskip\\\hfill What is wrong with this?}\bigskip\bigskip @@ -460,6 +506,28 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[c] + \frametitle{Coursework Dates} + +Similar to C++:\bigskip + +\begin{itemize} + \item Preliminary Parts: Wednesdays 4pm + \begin{itemize} + \item Preliminary Part 6: 3\% (13 November) + \item Preliminary Part 7: 4\% (20 November) + \item Preliminary Part 8: 4\% (27 November) + \item Preliminary Part 9: 4\% (5 December) + \end{itemize}\medskip + \item Core Part: 35\% (15 January 2020) + +\end{itemize} +\end{frame} + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\begin{frame}[c] \frametitle{Coursework} \begin{itemize} @@ -469,11 +537,14 @@ \item there is feedback when pushing code to github\medskip -\item there are \texttt{jar}-files you can use to test my implementation\bigskip +\item there are \texttt{jar}-files you can use to test + my reference implementation\bigskip -\item we want you to learn FP!\\ \alert{\bf no vars}, no mutable - data-structures\\ e.g.~no \texttt{Arrays}, no \texttt{ListBuffer} +\item we want you to learn FP!\smallskip\\ \alert{\bf no vars}, no mutable + data-structures\\ \quad{}e.g.~no \texttt{Arrays}, no \texttt{ListBuffer} \end{itemize} + +\LEFTarrow{1}{11}{11.8} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -659,10 +730,11 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[c] -\frametitle{Conclusion} +\frametitle{Conclusion for Today} \begin{itemize} -\item Scala is still under heavy development\\ (the compiler is terribly slow)\medskip +\item Scala is still under development, 2.13.1 came out in Sept.\\ + (the compiler is terribly slow)\medskip \item {\bf\url{http://www.scala-lang.org/}}\bigskip \item it is a rather \textbf{\alert{deep}} language\ldots i.e.~gives @@ -690,7 +762,7 @@ \begin{center} My Office Hours: Thursdays 12 -- 14\\ - And specifically for Scala: Tuesday 10:45 -- 11:45 + And specifically for Scala: Tuesdays 10:45 -- 11:45 \end{center} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%