--- 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]