progs/lecture2.scala
changeset 366 1c829680503e
parent 365 fc118ee0fce4
child 367 e6ae724255a8
--- a/progs/lecture2.scala	Mon Nov 23 02:43:03 2020 +0000
+++ b/progs/lecture2.scala	Tue Nov 24 09:04:06 2020 +0000
@@ -592,11 +592,11 @@
 def moves(xs: List[Int], n: Int) : List[List[Int]] = 
  (xs, n) match {
    case (Nil, _) => Nil
-   case (xs, 0) => Nil
+   case (_, 0) => Nil
    case (x::xs, n) => (x::xs) :: moves(xs, n - 1)
  }
 
-
+// List(5,5,1,0) -> moves(List(5,1,0), 5)
 moves(List(5,1,0), 1)
 moves(List(5,1,0), 2)
 moves(List(5,1,0), 5)
@@ -605,8 +605,9 @@
 
 def search(xs: List[Int]) : Boolean = xs match {
   case Nil => true
-  case (x::xs) =>
-    if (xs.length < x) true else moves(xs, x).exists(search(_))
+  case x::xs =>
+    if (xs.length < x) true 
+    else moves(xs, x).exists(search(_))
 }
 
 
@@ -621,6 +622,11 @@
 search(List(5,1,1))
 search(List(3,5,1,0,0,0,0,0,0,0,0,1))
 
+
+import scala.util._
+List.fill(100)(Random.nextInt(2))
+search(List.fill(100)(Random.nextInt(10)))
+
 // generate *all* jump tours
 //    if we are only interested in the shortes one, we could
 //    shortcircut the calculation and only return List(x) in
@@ -630,9 +636,10 @@
 
 def jumps(xs: List[Int]) : List[List[Int]] = xs match {
   case Nil => Nil
-  case (x::xs) => {
+  case x::xs => {
     val children = moves(xs, x)
-    val results = children.map(cs => jumps(cs).map(x :: _)).flatten
+    val results = 
+      children.map(cs => jumps(cs).map(x :: _)).flatten
     if (xs.length < x) List(x) :: results else results
   }
 }