--- 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)
//====================================