updated
authorChristian Urban <christian.urban@kcl.ac.uk>
Mon, 23 Nov 2020 02:43:03 +0000
changeset 365 fc118ee0fce4
parent 364 f1a6fa599d26
child 366 1c829680503e
updated
progs/lecture2.scala
slides/slides02.tex
slides/slides03.pdf
slides/slides03.tex
--- a/progs/lecture2.scala	Sun Nov 22 03:45:22 2020 +0000
+++ b/progs/lecture2.scala	Mon Nov 23 02:43:03 2020 +0000
@@ -392,9 +392,9 @@
 // Pattern Matching
 //==================
 
-// A powerful tool which is supposed to come to Java in a few years
-// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
-// has it for many years ;o)
+// A powerful tool which is supposed to come to Java in 
+// a few years time (https://www.youtube.com/watch?v=oGll155-vuQ).
+// ...Scala already has it for many years ;o)
 
 // The general schema:
 //
@@ -407,24 +407,20 @@
 
 
 // recall
-val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
-
-def my_flatten(xs: List[Option[Int]]): List[Int] = 
-xs match {
-  case Nil => Nil 
-  case None::rest => my_flatten(rest)
-  case Some(v)::rest => v :: my_flatten(rest)
-}
+def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = 
+  lst match {
+    case Nil => Nil
+    case x::xs => f(x)::my_map_int(xs, f)
+  }
 
-my_flatten(List(None, Some(1), Some(2), None, Some(3)))
-
+def my_map_option(o: Option[Int], f: Int => Int) : Option[Int] = 
+  o match {
+    case None => None
+    case Some(x) => Some(f(x))
+  }
 
-// another example with a default case
-def get_me_a_string(n: Int): String = n match {
-  case 0 | 1 | 2 => "small"
-}
-
-get_me_a_string(3)
+my_map_option(None, x => x * x)
+my_map_option(Some(8), x => x * x)
 
 
 // you can also have cases combined
@@ -434,20 +430,17 @@
   case "September" | "October" | "November" => "It's autumn"
   case "December" => "It's winter"
   case "January" | "February" => "It's unfortunately winter"
-}
- 
-println(season("November"))
-
-// What happens if no case matches?
-println(season("foobar"))
-
-
-// days of some months
-def days(month: String) : Int = month match {
-  case "March" | "April" | "May" => 31
-  case "June" | "July" | "August" => 30
+  case _ => "Wrong month"
 }
 
+// pattern-match on integers
+
+def fib(n: Int) : Int = n match { 
+  case 0 | 1 => 1
+  case n => fib(n - 1) + fib(n - 2)
+}
+
+fib(10)
 
 // Silly: fizz buzz
 def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
@@ -457,22 +450,31 @@
   case _ => n.toString  
 }
 
-for (n <- 0 to 20) 
+for (n <- 1 to 20) 
  println(fizz_buzz(n))
 
 
+val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
+
+def my_flatten(xs: List[Option[Int]]): List[Int] = 
+ xs match {
+   case Nil => Nil 
+   case None::rest => my_flatten(rest)
+   case Some(v)::rest => v :: my_flatten(rest)
+ }
+
+my_flatten(List(None, Some(1), Some(2), None, Some(3)))
+
+
+
+ 
+
+
 
 
 // Recursion
 //===========
 
-// well-known example
-
-def fib(n: Int) : Int = { 
-  if (n == 0 || n == 1) 1
-   else fib(n - 1) + fib(n - 2)
-}
-
 
 /* Say you have characters a, b, c.
    What are all the combinations of a certain length?
--- a/slides/slides02.tex	Sun Nov 22 03:45:22 2020 +0000
+++ b/slides/slides02.tex	Mon Nov 23 02:43:03 2020 +0000
@@ -435,33 +435,34 @@
 
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[t]
+% \begin{frame}[t]
 
-  \begin{center}  
-  \includegraphics[scale=0.3]{../pics/blow.png}
-  \end{center}
+%   \begin{center}  
+%   \includegraphics[scale=0.3]{../pics/blow.png}
+%   \end{center}
   
-  \begin{textblock}{14}(2,11.4)
-  \large\bf{}Mind-Blowing Programming Languages:\\ 
-  Overloading in any language is great but it makes a difference\; \code{10/3}
-  \;or\; \code{10.0/3}
-  \end{textblock}
-  \end{frame}
+%   \begin{textblock}{14}(2,11.4)
+%   \large\bf{}Mind-Blowing Programming Languages:\\ 
+%   Overloading in any language is great but it makes a difference\; \code{10/3}
+%   \;or\; \code{10.0/3}
+%   \end{textblock}
+%   \end{frame}
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[t]
+% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% \begin{frame}[t]
 
-  \begin{center}  
-  \includegraphics[scale=0.3]{../pics/blow.png}
-  \end{center}
+%   \begin{center}  
+%   \includegraphics[scale=0.3]{../pics/blow.png}
+%   \end{center}
   
-  \begin{textblock}{14}(2,11.4)
-  \large\bf{}Mind-Blowing Programming Languages:\\ 
-  \centering PHP \textcolor{gray}{(7.0)}
-  \end{textblock}
-  \end{frame}
-  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%   \begin{textblock}{14}(2,11.4)
+%   \large\bf{}Mind-Blowing Programming Languages:\\ 
+%   \centering PHP \textcolor{gray}{(7.0)}
+%   \end{textblock}
+%   \end{frame}
+%   %%%
+  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 
Binary file slides/slides03.pdf has changed
--- a/slides/slides03.tex	Sun Nov 22 03:45:22 2020 +0000
+++ b/slides/slides03.tex	Mon Nov 23 02:43:03 2020 +0000
@@ -1,5 +1,5 @@
 % !TEX program = xelatex
-\documentclass[dvipsnames,14pt,t,xelatex]{beamer}
+\documentclass[dvipsnames,14pt,t,xelatex,aspectratio=169,xcolor={table}]{beamer}
 \usepackage{../slides}
 \usepackage{../graphics}
 \usepackage{../langs}
@@ -52,10 +52,10 @@
   \begin{center}
   \begin{tabular}{ll}
     Email:  & christian.urban at kcl.ac.uk\\
-    Office: & N\liningnums{7.07} (North Wing, Bush House)\bigskip\\
+    %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\\ 
+    %Office Hours: &  Thursdays 12:00 -- 14:00\\
+    %Additionally: & (for Scala) Tuesdays 10:45 -- 11:45\\ 
   \end{tabular}
   \end{center}
 
@@ -63,18 +63,18 @@
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
-\begin{frame}[c]
-\frametitle{Preliminary 6}
+% \begin{frame}[c]
+% \frametitle{Preliminary 6}
 
-Raw marks (261 submissions):\bigskip
+% Raw marks (261 submissions):\bigskip
 
-\begin{itemize}
-\item 3\%: \hspace{4mm}219
-\item 2\%: \hspace{4mm}19
-\item 1\%: \hspace{4mm}0
-\item 0\%: \hspace{4mm}23 \;(4 no submission)
-\end{itemize}  
-\end{frame}
+% \begin{itemize}
+% \item 3\%: \hspace{4mm}219
+% \item 2\%: \hspace{4mm}19
+% \item 1\%: \hspace{4mm}0
+% \item 0\%: \hspace{4mm}23 \;(4 no submission)
+% \end{itemize}  
+% \end{frame}
 
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -320,35 +320,35 @@
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
-\begin{frame}[c,fragile]
-\frametitle{Sudoku}
+% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+% \begin{frame}[c,fragile]
+% \frametitle{Sudoku}
 
-A very simple-minded version on 110 problems:\bigskip
+% A very simple-minded version on 110 problems:\bigskip
 
-\begin{itemize}
-\item 1 core: 800 secs
-\item 2 cores: 400 secs
-\item 8 cores: 290 secs
-\item 18 cores: 142 secs
-\end{itemize}
+% \begin{itemize}
+% \item 1 core: 800 secs
+% \item 2 cores: 400 secs
+% \item 8 cores: 290 secs
+% \item 18 cores: 142 secs
+% \end{itemize}
 
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% \end{frame}
+% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[t]
+% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% \begin{frame}[t]
 
-  \begin{center}  
-  \includegraphics[scale=0.3]{../pics/blow.png}
-  \end{center}
+%   \begin{center}  
+%   \includegraphics[scale=0.3]{../pics/blow.png}
+%   \end{center}
   
-  \begin{textblock}{14}(2,11.4)
-  \large\bf{}Mind-Blowing Programming Languages:\\ 
-  \centering JavaScript
-  \end{textblock}
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%   \begin{textblock}{14}(2,11.4)
+%   \large\bf{}Mind-Blowing Programming Languages:\\ 
+%   \centering JavaScript
+%   \end{textblock}
+% \end{frame}
+% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 \end{document}