Binary file pics/fun.png has changed
--- a/pre_testing1/collatz.scala Mon Nov 09 17:49:12 2020 +0000
+++ b/pre_testing1/collatz.scala Sat Nov 14 00:40:47 2020 +0000
@@ -21,7 +21,6 @@
//collatz_max(10000000)
//collatz_max(100000000)
-
/* some test cases
val bnds = List(10, 100, 1000, 10000, 100000, 1000000)
@@ -43,7 +42,7 @@
//for (i <- 130 to 10000) println(s"$i: ${last_odd(i)}")
-for (i <- 1 to 100) println(s"$i: ${collatz(i)}")
+//for (i <- 1 to 100) println(s"$i: ${collatz(i)}")
}
--- a/pre_testing1/collatz_test.sh Mon Nov 09 17:49:12 2020 +0000
+++ b/pre_testing1/collatz_test.sh Sat Nov 14 00:40:47 2020 +0000
@@ -7,7 +7,6 @@
out=${1:-output}
echo -e "" > $out
-echo -e "" > c$out
echo -e "Below is the feedback for your submission collatz.scala" >> $out
echo -e "" >> $out
@@ -16,13 +15,13 @@
# compilation tests
function scala_compile {
- (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -Xprint:parser "$1" 2>> $out 1>> $out)
+ (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -Xprint:parser "$1" 2> c$out 1> c$out)
}
# functional tests
function scala_assert {
- (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -i "$1" -- "$2" -e "" 2> /dev/null 1> /dev/null)
+ (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -nc -i "$1" -- "$2" -e "" 2> /dev/null 1> /dev/null)
}
# purity test
@@ -38,7 +37,7 @@
if (scala_compile collatz.scala)
then
- echo -e " --> passed" >> $out
+ echo -e " --> success" >> $out
tsts=$(( 0 ))
else
echo -e " --> SCALA DID NOT RUN collatz.scala\n" >> $out
@@ -59,7 +58,7 @@
echo -e " --> FAIL (make triple-sure your program conforms to the required format)\n" >> $out
tsts=$(( 1 ))
else
- echo -e " --> passed" >> $out
+ echo -e " --> success" >> $out
tsts=$(( 0 ))
fi
fi
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/expr.scala Sat Nov 14 00:40:47 2020 +0000
@@ -0,0 +1,50 @@
+// Scala is about expressions
+//
+//
+
+1 + 2
+
+val r1 = 1 + 2
+val r2 = r1 * r1
+
+val new_list =
+ for (n <- (1 to 10).toList) yield n * n
+
+
+def my_not_equal(x: Int, y: Int) : Boolean = {
+ !(x == y)
+}
+
+
+// why return is not needed in Scala
+
+def sum_even(ls: List[Int]): Int = {
+ val aux = for (x <- ls) yield {
+ if (x % 2 == 0) x else 0
+ }
+ aux.sum
+}
+
+sum_even(List(1,2,3,4,5,6))
+
+def sum_return(ls: List[Int]): Int = {
+ val aux = for (x <- ls) yield {
+ if (x % 2 == 0) (return x) else (return 0)
+ }
+ aux.sum[Int]
+}
+
+sum_return(List(2,3,4,5,6))
+
+
+// replace subexpressions should not
+// change the meaning, but with return it does:
+
+def sq1(n: Int): Int = n * n
+def sq2(n: Int): Int = return n * n
+
+def sum_squares(ls: List[Int]): Int = {
+ (for (n <- ls) yield (return n * n)).sum[Int]
+}
+
+sum_squares(List(1,2,3,4,5,6))
\ No newline at end of file
--- a/progs/lecture1.scala Mon Nov 09 17:49:12 2020 +0000
+++ b/progs/lecture1.scala Sat Nov 14 00:40:47 2020 +0000
@@ -2,16 +2,19 @@
//=================
+
// Value assignments
// (their names should be lower case)
//====================================
+
val x = 42
val y = 3 + 4
val z = x / y
val x = 70
print(z)
+
// (you cannot reassign values: z = 9 will give an error)
//var z = 9
//z = 10
@@ -292,26 +295,29 @@
// For-Comprehensions (not For-Loops)
//====================================
-(1 to 10).toList
-for (n <- (1 to 10).toList) yield {
- square(n) + 1
+val lst = (1 to 10).toList
+for (n <- lst) yield n * n
+
+
+for (n <- lst) yield {
+ square(n) + double(n)
}
for (n <- (1 to 10).toList;
- m <- (1 to 10).toList) yield (m, n)
+ m <- (1 to 5).toList) yield (n, m, n * m)
// you can assign the result of a for-comprehension
// to a value
val mult_table =
for (n <- (1 to 10).toList;
- m <- (1 to 10).toList) yield m * n
+ m <- (1 to 10).toList) yield n * m
println(mult_table.mkString)
mult_table.sliding(10,10).mkString("\n")
-// the list/set/... can also be constructed in any
-// other way
+// for-comprehensions also work for other
+// collections
for (n <- Set(10,12,4,5,7,8,10)) yield n * n
@@ -319,17 +325,14 @@
n * n
}
-if (1 == 2) "a" else "b"
+// with if-predicates / filters
-// with if-predicates / filters
+if (1 == 2) "a" else "b"
for (n <- (1 to 3).toList;
m <- (1 to 3).toList;
if (n + m) % 2 == 0) yield (n, m)
-for (n <- (1 to 3).toList;
- m <- (1 to 3).toList;
- if ((((n + m) % 2 == 0)))) yield (n, m)
// with patterns
@@ -343,11 +346,31 @@
// general pattern of for-yield
// (yield can be several lines)
-for (p <- ...) yield {
+for (pat <- ...) yield {
// potentially complicated
// calculation of a result
}
+// For without yield
+//===================
+
+// with only a side-effect (no list is produced),
+// has no "yield"
+
+for (n <- (1 to 10).toList) println(n * n)
+
+for (n <- (1 to 10).toList) yield n * n
+
+// BTW: a roundabout way of printing out a list, say
+val lst = ('a' to 'm').toList
+
+for (i <- (0 until lst.length)) println(lst(i))
+
+// Why not just? Why making your life so complicated?
+for (c <- lst) println(c)
+
+
+
// Functions producing multiple outputs
//======================================
@@ -371,23 +394,6 @@
strs.maxBy(_._1)
-// For without yield
-//===================
-
-// with only a side-effect (no list is produced),
-// has no "yield"
-
-for (n <- (1 to 10)) println(n)
-
-(1 to 10).toList
-(1 until 10).toList
-// BTW: a roundabout way of printing out a list, say
-val lst = ('a' to 'm').toList
-
-for (i <- (0 until lst.length)) println(lst(i))
-
-// Why not just? Why making your life so complicated?
-for (c <- lst) println(c)
Binary file slides/slides01.pdf has changed
--- a/slides/slides01.tex Mon Nov 09 17:49:12 2020 +0000
+++ b/slides/slides01.tex Sat Nov 14 00:40:47 2020 +0000
@@ -248,8 +248,8 @@
My personal keboard shortcut for VS Code\\
(in keybindings.json)\bigskip
-\tiny
-\begin{lstlisting}[language=json,numbers=none]
+\footnotesize
+\begin{lstlisting}[language=json,numbers=none,xrightmargin=-4cm]
[
{
"key": "ctrl+enter",
@@ -913,9 +913,16 @@
%\end{textblock}
%\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
+\begin{frame}[c]
+\begin{center}
+\includegraphics[scale=0.4]{../pics/fun.png}
+\end{center}
+\end{frame}
+\begin{frame}<1-10>[t]
+
+\end{frame}
\end{document}
Binary file slides/slides02.pdf has changed
--- a/slides/slides02.tex Mon Nov 09 17:49:12 2020 +0000
+++ b/slides/slides02.tex Sat Nov 14 00:40:47 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{chessboard}
%\usepackage[LSBC4,T1]{fontenc}
\usepackage{../slides}
@@ -46,10 +46,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}
@@ -57,254 +57,31 @@
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c,fragile]
- \frametitle{Scala 2.13.1}
-
- \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{Reference Implementation}
-
-Keep your implementation and my reference implementation separate.\bigskip
-
- \begin{lstlisting}[language={},numbers=none,
- basicstyle=\ttfamily\small,xleftmargin=-2mm]
- $ scala -cp collatz.jar
-
- scala> CW6a.collatz(6)
- res0: Long = 8
-
-
- scala> import CW6a._
- scala> collatz(9)
- res1: Long = 19
- \end{lstlisting}%$
+\begin{frame}[t,fragile]
+\frametitle{For-Comprehensions}
-\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]
- \frametitle{Discussion Forum}
-
- \large
- ``Since we can't use \code{var}s I was wondering if we could use a stack?''
- \bigskip\bigskip\bigskip\bigskip
+%\small
+\begin{lstlisting}[language=Scala,numbers=none]
+for (n <- List(1, 2, 3, 4, 5)) yield n * n
+\end{lstlisting}
- \small
- My \pcode{collatz} and \pcode{collatz_max} functions are 4 loc each.
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\begin{textblock}{5}(2,6)
+\includegraphics[scale=0.3]{../pics/fun.png}
+\end{textblock}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[t]
- \frametitle{Email: Hate 'val'}
-
- \mbox{}\\[-22mm]\mbox{}
-
- \begin{center}
- \begin{bubble}[10.5cm]
- Subject: \textbf{Hate '\textbf{\texttt{val}}'}\hfill 01:00 AM\medskip\\
-
- Hello Mr Urban,\medskip\\
-
- I just wanted to ask, how are we suppose to work
- with the completely useless \textbf{\texttt{val}}, that can’t be changed ever? Why is
- this rule active at all? I’ve spent 4 hours not thinking on the
- coursework, but how to bypass this annoying rule. What’s the whole
- point of all these coursework, when we can’t use everything Scala
- gives us?!?\medskip\\
-
- Regards.\\
- \mbox{}\hspace{5mm}\textcolor{black!50}{<<deleted>>}\\
- \end{bubble}
- \end{center}
-
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c]
+\begin{textblock}{5}(9,6)
+\includegraphics[scale=0.3]{../pics/fun.png}
+\end{textblock}
- \mbox{}\\[-25mm]\mbox{}
-
-\begin{center}
- \begin{bubble}[10.5cm]
- Subject: \textbf{Re: Hate '\textbf{\texttt{val}}'}\hfill 01:02 AM\bigskip\bigskip\\
-
- \textcolor{black!70}{
- \textit{\large<<my usual rant about fp\ldots\\ concurrency bla bla\ldots{} better programs
- yada>>}}\bigskip\bigskip\bigskip
-
- PS: What are you trying to do where you desperately want to use \texttt{var}?
- \end{bubble}
-\end{center}
\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c,fragile]
-
-\begin{textblock}{6}(0.5,0.5)
-\begin{bubble}[11.5cm]
- \small
- Subject: \textbf{Re: Re: Hate '\textbf{\texttt{val}}'}\hfill 01:04 AM\medskip\\
-
- \textbf{Right now my is\_legal function works fine:}
-
-\footnotesize\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
- def is_legal(dim: Int, path: Path)(x: Pos): Boolean = {
- var boolReturn = false
- if(x._1 > dim || x._2 > dim || x._1 < 0 || x._2 < 0) {
- else { var breakLoop = false
- if(path == Nil) { boolReturn = true }
- else { for(i <- 0 until path.length) {
- if(breakLoop == false) {
- if(path(i) == x) {
- boolReturn = true
- breakLoop = true
- }
- else { boolReturn = false }
- } else breakLoop
- }
- }
- boolReturn
- }
-\end{lstlisting}
-\end{bubble}
-\end{textblock}
-
-\begin{textblock}{6}(8.2,11.8)
-\begin{bubble}[5.5cm]\footnotesize\bf
-\ldots{}but I can’t make it work with boolReturn being val. What approach would
-you recommend in this case, and is using var in this case justified?
-\end{bubble}
-\end{textblock}
-
-\only<2>{
-\begin{textblock}{6}(0.3,11.8)
- \begin{bubble}[3.1cm]
- \textbf{Me:}
- \raisebox{-12mm}{\includegraphics[scale=0.08]{../pics/throwup.jpg}}
- \end{bubble}
-\end{textblock}}
-
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[t,fragile]
-
-\mbox{}\\[-25mm]\mbox{}
-
-\begin{textblock}{6}(0.5,2)
- \begin{bubble}[11.5cm]
- Subject: \textbf{Re: Re: Re: Hate '\textbf{\texttt{val}}'}\hfill 01:06 AM\bigskip\\
- \small
-
- OK. So you want to make sure that the \texttt{x}-position is not outside the
- board....and furthermore you want to make sure that the \texttt{x}-position
- is not yet in the path list. How about something like\bigskip
-
-\footnotesize\begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm]
- def is_legal(dim: Int, path: Path)(x: Pos): Boolean =
- ...<<some board conditions>>... && !path.contains(x)
-\end{lstlisting}\bigskip
-
- \small Does not even contain a \texttt{val}.
- \end{bubble}
-\end{textblock}
-
-\begin{textblock}{6}(7,12)
-\footnotesize\textcolor{black!50}{(This is all on one line)}
-\end{textblock}
-
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[t,fragile]
-
-\mbox{}\\[-15mm]\mbox{}
-
-\begin{textblock}{6}(1,3)
- \begin{bubble}[10.5cm]
- Subject: \textbf{Re: Re: Re: Re: Hate '\textbf{\texttt{val}}'}\hfill 11:02 AM\bigskip\bigskip\\
-
- THANK YOU! You made me change my coding perspective. Because of you,
- I figured out the next one\ldots
- \end{bubble}
-\end{textblock}
-
-\only<2>{
-\begin{textblock}{6}(0.3,11.8)
- \begin{bubble}[3.1cm]
- \textbf{Me:}
- \raisebox{-12mm}{\includegraphics[scale=0.15]{../pics/happy.jpg}}
- \end{bubble}
-\end{textblock}}
-
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}[c]
-\frametitle{Assignments}
-
-Don't change any names or types in the templates!\bigskip
-
-Avoid at all costs:
-
-\begin{itemize}
-\item \code{var}
-\item \code{return}
-\item \texttt{ListBuffer}
-\item \texttt{mutable}
-\item \texttt{.par}
-\end{itemize}\bigskip\bigskip
-
-I cannot think of a good reason to use stacks.
-\end{frame}
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t]
-\frametitle{For-Comprehensions Again}
+\frametitle{For-Comprehensions}
\begin{center}
\begin{tikzpicture}[scale=1,