--- a/progs/lecture2.scala Sun Nov 03 14:42:17 2019 +0000
+++ b/progs/lecture2.scala Mon Nov 04 00:51:10 2019 +0000
@@ -525,6 +525,45 @@
}
+// Recursion
+//===========
+
+/* a, b, c
+
+aa aaa
+ab baa
+ac caa
+ba => ......
+bb
+bc
+ca
+cb
+cc
+
+*/
+
+def perms(cs: List[Char], l: Int) : List[String] = {
+ if (l == 0) List("")
+ else for (c <- cs; s <- perms(cs, l - 1)) yield s"$c$s"
+}
+
+perms("abc".toList, 2)
+
+def move(from: Char, to: Char) =
+ println(s"Move disc from $from to $to!")
+
+def hanoi(n: Int, from: Char, via: Char, to: Char) : Unit = {
+ if (n == 0) ()
+ else {
+ hanoi(n - 1, from, to, via)
+ move(from, to)
+ hanoi(n - 1, via, from, to)
+ }
+}
+
+hanoi(40, 'A', 'B', 'C')
+
+
// Tail Recursion
//================