--- a/progs/lecture2.scala Mon Nov 04 00:51:10 2019 +0000
+++ b/progs/lecture2.scala Mon Nov 04 11:48:37 2019 +0000
@@ -21,6 +21,8 @@
// (needs a library and 'magic' option -Yrepl-class-based)
+
+
// Just for Fun: Mutable vs Immutable
//====================================
//
@@ -70,6 +72,38 @@
for (n <- List(1, 2, 3, 4, 5)) println(n)
+// String Interpolations
+//=======================
+
+val n = 3
+println("The square of " + n + " is " + square(n) + ".")
+
+println(s"The square of ${n} is ${square(n)}.")
+
+
+// helpful for debugging purposes
+//
+// "The most effective debugging tool is still careful thought,
+// coupled with judiciously placed print statements."
+// — Brian W. Kernighan, in Unix for Beginners (1979)
+
+
+def gcd_db(a: Int, b: Int) : Int = {
+ println(s"Function called with ${a} and ${b}.")
+ if (b == 0) a else gcd_db(b, a % b)
+}
+
+gcd_db(48, 18)
+
+
+// Asserts/Testing
+//=================
+
+assert(gcd(48, 18) == 6)
+
+assert(gcd(48, 18) == 5, "The gcd test failed")
+
+
// Higher-Order Functions
//========================