# HG changeset patch # User Christian Urban # Date 1480612092 0 # Node ID ea0d0a862a9c6466fa4f8a88334e263eae9c1820 # Parent 2d57b0d43a0fad2a13a73f494db8bde47b904cf8# Parent 3cbe3d90b77f2bd00c154d1afbab37d66a524636 merged diff -r 2d57b0d43a0f -r ea0d0a862a9c progs/lecture2.scala --- a/progs/lecture2.scala Thu Dec 01 17:08:02 2016 +0000 +++ b/progs/lecture2.scala Thu Dec 01 17:08:12 2016 +0000 @@ -180,9 +180,6 @@ - - - def my_flatten(lst: List[Option[Int]]): List[Int] = lst match { case Nil => Nil case None::xs => my_flatten(xs) @@ -200,6 +197,20 @@ get_me_a_string(0) +// you can also have cases combined +def season(month: String) = month match { + case "March" | "April" | "May" => "It's spring" + case "June" | "July" | "August" => "It's summer" + case "September" | "October" | "November" => "It's autumn" + case "December" | "January" | "February" => "It's winter" +} + +println(season("November")) + +// What happens if no case matches? + +println(season("foobar")) + // User-defined Datatypes //======================== diff -r 2d57b0d43a0f -r ea0d0a862a9c progs/lecture3.scala --- a/progs/lecture3.scala Thu Dec 01 17:08:02 2016 +0000 +++ b/progs/lecture3.scala Thu Dec 01 17:08:12 2016 +0000 @@ -116,9 +116,17 @@ searchT(111, junk_coins, start_acc) searchT(111111, junk_coins, start_acc) -// moral: whenever a recursive function is resource-critical -// (i.e. works on large recursion depth), then you need to -// write it in tail-recursive fashion +// Moral: Whenever a recursive function is resource-critical +// (i.e. works with large recursion depths), then you need to +// write it in tail-recursive fashion. +// +// Unfortuantely, the Scala is because of current limitations in +// the JVM not as clever as other functional languages. It can +// only optimise "self-tail calls". This excludes the cases of +// multiple functions making tail calls to each other. Well, +// nothing is perfect. + + // Polymorphic Types