updated
authorChristian Urban <christian.urban@kcl.ac.uk>
Sat, 28 Nov 2020 12:09:55 +0000
changeset 375 ddda2e76a40f
parent 374 90b267768329
child 376 6cc36d0ef79e
updated
pre_marking1/collatz_test.sh
progs/lecture3.scala
--- a/pre_marking1/collatz_test.sh	Fri Nov 27 15:03:50 2020 +0000
+++ b/pre_marking1/collatz_test.sh	Sat Nov 28 12:09:55 2020 +0000
@@ -69,6 +69,8 @@
       echo -e "  --> success" >> $out
       tsts=$(( 0 )) 
    fi
+else
+   tsts=$(( 1 ))  
 fi    
 
 
--- a/progs/lecture3.scala	Fri Nov 27 15:03:50 2020 +0000
+++ b/progs/lecture3.scala	Sat Nov 28 12:09:55 2020 +0000
@@ -213,18 +213,13 @@
 // Tail recursion
 //================
 
-
-def fact(n: Long): Long = 
+def fact(n: BigInt): BigInt = 
   if (n == 0) 1 else n * fact(n - 1)
 
-def factB(n: BigInt): BigInt = 
-  if (n == 0) 1 else n * factB(n - 1)
-
-factB(100000)
-
 fact(10)              //ok
 fact(10000)           // produces a stackoverflow
 
+
 def factT(n: BigInt, acc: BigInt): BigInt =
   if (n == 0) acc else factT(n - 1, n * acc)
 
@@ -246,7 +241,18 @@
 // call; Scala can do this only for tail-recursive
 // functions
 
+def length(xs: List[Int]) : Int = xs match {
+  case Nil => 0
+  case _ :: tail => 1 + length(tail)
+}
 
+@tailrec
+def lengthT(xs: List[Int], acc : Int) : Int = xs match {
+  case Nil => acc
+  case _ :: tail => lengthT(tail, 1 + acc)
+}
+
+lengthT(List.fill(10000000)(1), 0)
 
 
 // Sudoku
@@ -295,6 +301,7 @@
       map(c => search(update(game, empty(game), c))).toList.flatten
 }
 
+
 def search1T(games: List[String]): Option[String] = games match {
   case Nil => None
   case game::rest => {