updated
authorChristian Urban <christian.urban@kcl.ac.uk>
Mon, 09 Nov 2020 17:49:12 +0000
changeset 359 8aaf187d25f0
parent 358 542d2cf143b0
child 360 e45d2890749d
updated
progs/lecture1.scala
slides/slides01.pdf
slides/slides01.tex
--- a/progs/lecture1.scala	Mon Nov 09 14:36:35 2020 +0000
+++ b/progs/lecture1.scala	Mon Nov 09 17:49:12 2020 +0000
@@ -244,19 +244,21 @@
 /* boolean operators
  
    ==     equals
+   !=     not equals
    !      not
    && ||  and, or
 */
 
 
-def fact2(n: BigInt) : BigInt = 
-  if (n == 0) 1 else n * fact2(n - 1)
 
-fact2(150)
-
-def fib(n: Int) : Int =
+def fib(n: Int) : Int = {
   if (n == 0) 1 else
     if (n == 1) 1 else fib(n - 1) + fib(n - 2)
+}
+
+fib(9)
+
+
 
 
 //gcd - Euclid's algorithm
@@ -278,6 +280,14 @@
 // "last" line (expression) in a function determines the 
 // result
 
+def average(xs: List[Int]) : Int = {
+  if (xs.length == 0) 0 
+  else xs.sum / xs.length
+}
+
+average(List())
+
+
 
 // For-Comprehensions (not For-Loops)
 //====================================
Binary file slides/slides01.pdf has changed
--- a/slides/slides01.tex	Mon Nov 09 14:36:35 2020 +0000
+++ b/slides/slides01.tex	Mon Nov 09 17:49:12 2020 +0000
@@ -606,6 +606,7 @@
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \begin{frame}[c, fragile]
+%%\frametitle{General Scheme of}  
 
  \small
 \begin{lstlisting}[language=Scala,numbers=none]
@@ -620,6 +621,23 @@
 \end{frame}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
+\begin{frame}[c, fragile]
+
+\begin{lstlisting}[language=Scala,numbers=none]
+def average(xs: List[Int]) : Int = {
+  val s = xs.sum
+  val n = xs.length
+  s / n
+}
+\end{lstlisting}
+
+  
+\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ 
+
+
+
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %\begin{frame}[c]