diff -r 29fc780ca130 -r fa7f7144f2bb progs/lecture4.scala --- a/progs/lecture4.scala Tue Dec 07 01:35:00 2021 +0000 +++ b/progs/lecture4.scala Tue Dec 07 23:17:51 2021 +0000 @@ -1,6 +1,36 @@ // Scala Lecture 4 //================= +// tail-recursion +// polymorphic types +// implicits + +import scala.annotation.tailrec + +@tailrec +def fact(n: BigInt): BigInt = + if (n == 0) 1 else n * fact(n - 1) + +@tailrec +def factT(n: BigInt, acc: BigInt): BigInt = + if (n == 0) acc else factT(n - 1, n * acc) + + +println(factT(1000000), 1)) + + +def foo[A](args: List[A]) = ??? + +foo(List("1","2","3","4")) +import scala.annotation.tailrec + + +// from knight1.scala +def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = ??? + +// should be +def first[A, B](xs: List[A], f: A => Option[B]) : Option[B] = ??? + // expressions (essentially trees) @@ -252,7 +282,6 @@ // Tail recursion //================ -@tailrec def fact(n: BigInt): BigInt = if (n == 0) 1 else n * fact(n - 1) @@ -519,7 +548,7 @@ def increment = s.map(c => (c + 1).toChar) } -"HAL".increment +"HAL".increment.map(_.toInt) // Abstract idea: @@ -580,7 +609,7 @@ implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList) -val r1 = STAR("hello") +val r1 = STAR("ab") val r2 = STAR("hello") | STAR("world")