updated
authorChristian Urban <urbanc@in.tum.de>
Mon, 11 Nov 2019 13:24:12 +0000
changeset 316 8b57dd326a91
parent 315 7ea440e1ffbb
child 317 607ceabeeffc
updated
cws/cw02.pdf
cws/cw02.tex
progs/lecture1.scala
progs/lecture2.scala
slides/slides02.pdf
slides/slides02.tex
Binary file cws/cw02.pdf has changed
--- a/cws/cw02.tex	Sat Nov 09 22:04:53 2019 +0000
+++ b/cws/cw02.tex	Mon Nov 11 13:24:12 2019 +0000
@@ -156,6 +156,7 @@
   \texttt{overlap}(d_1, d_2) = \frac{d_1 \cdot d_2}{max(d_1^2, d_2^2)}  
   \]
 
+  where $d_1^2$ means $d_1 \cdot d_1$ and so on.
   You can expect this function to return a \texttt{Double} between 0 and 1. The
   overlap between the lists in (2) is $0.5384615384615384$.
 
--- a/progs/lecture1.scala	Sat Nov 09 22:04:53 2019 +0000
+++ b/progs/lecture1.scala	Mon Nov 11 13:24:12 2019 +0000
@@ -460,62 +460,6 @@
 
 test()
 
-// Option type
-//=============
-
-//in Java if something unusually happens, you return null or something
-//
-//in Scala you use Options instead
-//   - if the value is present, you use Some(value)
-//   - if no value is present, you use None
-
-
-List(7,2,3,4,5,6).find(_ < 4)
-List(5,6,7,8,9).find(_ < 4)
-
-
-
-// error handling with Options (no exceptions)
-//
-//  Try(something).getOrElse(what_to_do_in_case_of_an_exception)
-//
-import scala.util._
-import io.Source
-
-val my_url = "https://nms.kcl.ac.uk/christian.urban/"
-
-Source.fromURL(my_url).mkString
-
-Try(Source.fromURL(my_url).mkString).getOrElse("")
-
-Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None)
-
-
-// the same for files
-Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None)
-
-
-
-// how to implement a function for reading something from files...
-
-def get_contents(name: String) : List[String] = 
-  Source.fromFile(name).getLines.toList
-
-get_contents("test.txt")
-
-// slightly better - return Nil
-def get_contents(name: String) : List[String] = 
-  Try(Source.fromFile(name).getLines.toList).getOrElse(List())
-
-get_contents("text.txt")
-
-// much better - you record in the type that things can go wrong 
-def get_contents(name: String) : Option[List[String]] = 
-  Try(Some(Source.fromFile(name).getLines.toList)).getOrElse(None)
-
-get_contents("text.txt")
-get_contents("test.txt")
-
 
 
 
--- a/progs/lecture2.scala	Sat Nov 09 22:04:53 2019 +0000
+++ b/progs/lecture2.scala	Mon Nov 11 13:24:12 2019 +0000
@@ -1,64 +1,63 @@
 // Scala Lecture 2
 //=================
 
-// UNFINISHED BUSINESS from Lecture 1
-//====================================
 
 
-// for measuring time
-def time_needed[T](n: Int, code: => T) = {
-  val start = System.nanoTime()
-  for (i <- (0 to n)) code
-  val end = System.nanoTime()
-  (end - start) / 1.0e9
-}
+// The Option Type
+//=================
+
+// in Java, if something unusually happens, you return null or 
+// raise an exception
+//
+//in Scala you use Options instead
+//   - if the value is present, you use Some(value)
+//   - if no value is present, you use None
 
 
-val list = (1 to 1000000).toList
-time_needed(10, for (n <- list) yield n + 42)
-time_needed(10, for (n <- list.par) yield n + 42)
-
-// (needs a library and 'magic' option -Yrepl-class-based)
-
+List(7,2,3,4,5,6).find(_ < 4)
+List(5,6,7,8,9).find(_ < 4)
 
 
 
-// Just for Fun: Mutable vs Immutable
-//====================================
+// better error handling with Options (no exceptions)
+//
+//  Try(something).getOrElse(what_to_do_in_case_of_an_exception)
 //
-// - no vars, no ++i, no +=
-// - no mutable data-structures (no Arrays, no ListBuffers)
+
+import scala.util._
+import io.Source
+
+val my_url = "https://nms.kcl.ac.uk/christian.urban/"
+
+Source.fromURL(my_url).mkString
+
+Try(Source.fromURL(my_url).mkString).getOrElse("")
+
+Try(Some(Source.fromURL(my_url).mkString)).getOrElse(None)
 
 
-// Q: Count how many elements are in the intersections of 
-//    two sets?
+// the same for files
+Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None)
+
+// how to implement a function for reading something from files...
 
-def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
-  var count = 0
-  for (x <- A; if B contains x) count += 1 
-  count
-}
+def get_contents(name: String) : List[String] = 
+  Source.fromFile(name).getLines.toList
 
-val A = (1 to 1000).toSet
-val B = (1 to 1000 by 4).toSet
-
-count_intersection(A, B)
+get_contents("test.txt")
 
-// but do not try to add .par to the for-loop above
-
+// slightly better - return Nil
+def get_contents(name: String) : List[String] = 
+  Try(Source.fromFile(name).getLines.toList).getOrElse(List())
 
-//propper parallel version
-def count_intersection2(A: Set[Int], B: Set[Int]) : Int = 
-  A.par.count(x => B contains x)
+get_contents("text.txt")
 
-count_intersection2(A, B)
-
+// much better - you record in the type that things can go wrong 
+def get_contents(name: String) : Option[List[String]] = 
+  Try(Some(Source.fromFile(name).getLines.toList)).getOrElse(None)
 
-val A = (1 to 1000000).toSet
-val B = (1 to 1000000 by 4).toSet
-
-time_needed(100, count_intersection(A, B))
-time_needed(100, count_intersection2(A, B))
+get_contents("text.txt")
+get_contents("test.txt")
 
 
 
Binary file slides/slides02.pdf has changed
--- a/slides/slides02.tex	Sat Nov 09 22:04:53 2019 +0000
+++ b/slides/slides02.tex	Mon Nov 11 13:24:12 2019 +0000
@@ -1,3 +1,4 @@
+% !TEX program = xelatex
 \documentclass[dvipsnames,14pt,t,xelatex]{beamer}
 \usepackage{chessboard}
 \usepackage[LSBC4,T1]{fontenc}
@@ -38,9 +39,10 @@
   \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 Hours: &  Mondays 12:00 -- 14:00\\
+    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}
   \end{center}
 
@@ -48,24 +50,63 @@
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+\begin{frame}[c,fragile]
+  \frametitle{My Scala Version}
+  
+  \begin{lstlisting}[language={},numbers=none,
+    basicstyle=\ttfamily\small,xleftmargin=-2mm]
+  $ scala
+    
+  Welcome to Scala 2.13.1 (Java HotSpot(TM) 
+  64-Bit Server VM, Java 9). Type in expressions 
+  for evaluation. Or try :help.
+  
+  scala>
+  \end{lstlisting}%$
+  \bigskip\bigskip
+  
+  With older versions you will get strange results with my reference implementation.
+  
+  \end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 \begin{frame}[c,fragile]
-\frametitle{Scala on Lab Computers}
+\frametitle{Reference Implementation}
+  
+Keep your implementation and my reference implementation separate.\bigskip
 
-\begin{lstlisting}[language={},numbers=none,
-  basicstyle=\ttfamily\small,xleftmargin=-2mm]
-$ /usr/share/scala/bin/scala
+  \begin{lstlisting}[language={},numbers=none,
+    basicstyle=\ttfamily\small,xleftmargin=-2mm]
+  $ scala -cp collatz.jar
   
-Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit
-Server VM, Java 10.0.1). Type in expressions for
-evaluation. Or try :help.
+  scala> CW6a.collatz(6)
+  res0: Long = 8
+
 
-scala>
-\end{lstlisting}%$
+  scala> import CW6a._
+  scala> collatz(9)
+  res1: Long = 19
+  \end{lstlisting}%$
 
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+  
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
+\begin{frame}[t]
+  \frametitle{Preliminary Part 7}
+  
+  \Large
+  \[
+    \texttt{overlap}(d_1, d_2) = \frac{d_1 \cdot d_2}{max(d_1^2, d_2^2)}  
+  \]\bigskip
+
+  \large
+  \quad{}\;where \;$d_1^2$\; means \;$d_1 \cdot d_1$\; and so on
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+  
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
 \begin{frame}[c]
@@ -275,31 +316,23 @@
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c,fragile]
-\frametitle{\begin{tabular}{c}\\[1cm]\alert{Questions?}\end{tabular}}
+\begin{frame}[c]
+\frametitle{\begin{tabular}{c}\\[0cm]\alert{Questions?}\end{tabular}}
 
-%\begin{center}
-%\chessboard[maxfield=g7,
-%            color=blue!50,
-%            linewidth=0.2em,
-%            shortenstart=0.5ex,
-%            shortenend=0.5ex,
-%            markstyle=cross,
-%            markfields={a4, c4, Z3, d3, Z1, d1, a0, c0},
-%            color=red!50,
-%            markfields={f5, e6},
-%            boardfontsize=12pt,labelfontsize=8pt,
-%            setpieces={Ng7, Nb2},showmover=false]
-%\end{center}
-            
 \begin{center}
-My Office Hours: Mondays 12 -- 14
+  \begin{tabular}[t]{@{}l@{}l@{}}
+    \includegraphics[scale=0.1]{../pics/mand4.png} & \hspace{4mm}
+    \raisebox{0mm}{\includegraphics[scale=0.1]{../pics/mand3.png}}      
+  \end{tabular}     
+\end{center}
+
+\begin{center}
+  My Office Hours: Thursdays 12 -- 14\\
+  And specifically for Scala: Tuesdays 10:45 -- 11:45
 \end{center}
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
-\end{document}
 
 
 \end{document}