progs/lecture3.scala
changeset 325 ca9c1cf929fa
parent 323 1f8005b4cdf6
child 335 7e00d2b13b04
equal deleted inserted replaced
324:2969ee4a6cee 325:ca9c1cf929fa
   298 
   298 
   299 
   299 
   300 // User-defined Datatypes and Pattern Matching
   300 // User-defined Datatypes and Pattern Matching
   301 //=============================================
   301 //=============================================
   302 
   302 
   303 // trees
   303 
   304 
   304 
   305 
   305 
   306 
   306 // Tail recursion
   307 // expressions
   307 //================
   308 
   308 
   309 sealed abstract class Exp
   309 
   310 case class N(n: Int) extends Exp                  // for numbers
   310 def fact(n: Long): Long = 
   311 case class Plus(e1: Exp, e2: Exp) extends Exp
   311   if (n == 0) 1 else n * fact(n - 1)
   312 case class Times(e1: Exp, e2: Exp) extends Exp
   312 
   313 
   313 def factB(n: BigInt): BigInt = 
   314 def string(e: Exp) : String = e match {
   314   if (n == 0) 1 else n * factB(n - 1)
   315   case N(n) => s"$n"
   315 
   316   case Plus(e1, e2) => s"(${string(e1)} + ${string(e2)})" 
   316 factB(100000)
   317   case Times(e1, e2) => s"(${string(e1)} * ${string(e2)})"
   317 
   318 }
   318 fact(10)              //ok
   319 
   319 fact(10000)           // produces a stackoverflow
   320 val e = Plus(N(9), Times(N(3), N(4)))
   320 
   321 println(string(e))
   321 def factT(n: BigInt, acc: BigInt): BigInt =
   322 
   322   if (n == 0) acc else factT(n - 1, n * acc)
   323 def eval(e: Exp) : Int = e match {
   323 
   324   case N(n) => n
   324 factT(10, 1)
   325   case Plus(e1, e2) => eval(e1) + eval(e2) 
   325 println(factT(100000, 1))
   326   case Times(e1, e2) => eval(e1) * eval(e2) 
   326 
   327 }
   327 // there is a flag for ensuring a function is tail recursive
   328 
   328 import scala.annotation.tailrec
   329 println(eval(e))
   329 
   330 
   330 @tailrec
   331 def simp(e: Exp) : Exp = e match {
   331 def factT(n: BigInt, acc: BigInt): BigInt =
   332   case N(n) => N(n)
   332   if (n == 0) acc else factT(n - 1, n * acc)
   333   case Plus(e1, e2) => (simp(e1), simp(e2)) match {
   333 
   334     case (N(0), e2s) => e2s
   334 
   335     case (e1s, N(0)) => e1s
   335 
   336     case (e1s, e2s) => Plus(e1s, e2s)
   336 // for tail-recursive functions the Scala compiler
   337   }  
   337 // generates loop-like code, which does not need
   338   case Times(e1, e2) => (simp(e1), simp(e2)) match {
   338 // to allocate stack-space in each recursive
   339     case (N(0), _) => N(0)
   339 // call; Scala can do this only for tail-recursive
   340     case (_, N(0)) => N(0)
   340 // functions
   341     case (N(1), e2s) => e2s
   341 
   342     case (e1s, N(1)) => e1s
   342 // tail recursive version that searches 
   343     case (e1s, e2s) => Times(e1s, e2s)
   343 // for all solutions
   344   }  
   344 
   345 }
   345 def searchT(games: List[String], sols: List[String]): List[String] = games match {
   346 
   346   case Nil => sols
   347 
   347   case game::rest => {
   348 val e2 = Times(Plus(N(0), N(1)), Plus(N(0), N(9)))
   348     if (isDone(game)) searchT(rest, game::sols)
   349 println(string(e2))
   349     else {
   350 println(string(simp(e2)))
   350       val cs = candidates(game, emptyPosition(game))
   351 
   351       searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols)
   352 
   352     }
   353 // Tokens and Reverse Polish Notation
   353   }
   354 sealed abstract class Token
   354 }
   355 case class T(n: Int) extends Token
   355 
   356 case object PL extends Token
   356 searchT(List(game3), List()).map(pretty)
   357 case object TI extends Token
   357 
   358 
   358 
   359 def rp(e: Exp) : List[Token] = e match {
   359 // tail recursive version that searches 
   360   case N(n) => List(T(n))
   360 // for a single solution
   361   case Plus(e1, e2) => rp(e1) ::: rp(e2) ::: List(PL) 
   361 
   362   case Times(e1, e2) => rp(e1) ::: rp(e2) ::: List(TI) 
   362 def search1T(games: List[String]): Option[String] = games match {
   363 }
   363   case Nil => None
   364 println(string(e2))
   364   case game::rest => {
   365 println(rp(e2))
   365     if (isDone(game)) Some(game)
   366 
   366     else {
   367 def comp(ls: List[Token], st: List[Int]) : Int = (ls, st) match {
   367       val cs = candidates(game, emptyPosition(game))
   368   case (Nil, st) => st.head 
   368       search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
   369   case (T(n)::rest, st) => comp(rest, n::st)
   369     }
   370   case (PL::rest, n1::n2::st) => comp(rest, n1 + n2::st)
   370   }
   371   case (TI::rest, n1::n2::st) => comp(rest, n1 * n2::st)
   371 }
   372 }
   372 
   373 
   373 search1T(List(game3)).map(pretty)
   374 comp(rp(e), Nil)
   374 time_needed(10, search1T(List(game3)))
   375 
   375 
   376 def proc(s: String) : Token = s match {
       
   377   case  "+" => PL
       
   378   case  "*" => TI
       
   379   case  _ => T(s.toInt) 
       
   380 }
       
   381 
       
   382 comp("1 2 + 4 * 5 + 3 +".split(" ").toList.map(proc), Nil)
       
   383 
       
   384 
       
   385 
       
   386 
       
   387 // Sudoku 
       
   388 //========
       
   389 
       
   390 // THE POINT OF THIS CODE IS NOT TO BE SUPER
       
   391 // EFFICIENT AND FAST, just explaining exhaustive
       
   392 // depth-first search
       
   393 
       
   394 
       
   395 val game0 = """.14.6.3..
       
   396               |62...4..9
       
   397               |.8..5.6..
       
   398               |.6.2....3
       
   399               |.7..1..5.
       
   400               |5....9.6.
       
   401               |..6.2..3.
       
   402               |1..5...92
       
   403               |..7.9.41.""".stripMargin.replaceAll("\\n", "")
       
   404 
       
   405 type Pos = (Int, Int)
       
   406 val EmptyValue = '.'
       
   407 val MaxValue = 9
       
   408 
       
   409 val allValues = "123456789".toList
       
   410 val indexes = (0 to 8).toList
       
   411 
       
   412 
       
   413 def empty(game: String) = game.indexOf(EmptyValue)
       
   414 def isDone(game: String) = empty(game) == -1 
       
   415 def emptyPosition(game: String) = 
       
   416   (empty(game) % MaxValue, empty(game) / MaxValue)
       
   417 
       
   418 
       
   419 def get_row(game: String, y: Int) = 
       
   420   indexes.map(col => game(y * MaxValue + col))
       
   421 def get_col(game: String, x: Int) = 
       
   422   indexes.map(row => game(x + row * MaxValue))
       
   423 
       
   424 def get_box(game: String, pos: Pos): List[Char] = {
       
   425     def base(p: Int): Int = (p / 3) * 3
       
   426     val x0 = base(pos._1)
       
   427     val y0 = base(pos._2)
       
   428     val ys = (y0 until y0 + 3).toList
       
   429     (x0 until x0 + 3).toList.flatMap(x => ys.map(y => game(x + y * MaxValue)))
       
   430 }
       
   431 
       
   432 //get_row(game0, 0)
       
   433 //get_row(game0, 1)
       
   434 //get_col(game0, 0)
       
   435 //get_box(game0, (3, 1))
       
   436 
       
   437 
       
   438 // this is not mutable!!
       
   439 def update(game: String, pos: Int, value: Char): String = 
       
   440   game.updated(pos, value)
       
   441 
       
   442 def toAvoid(game: String, pos: Pos): List[Char] = 
       
   443   (get_col(game, pos._1) ++ get_row(game, pos._2) ++ get_box(game, pos))
       
   444 
       
   445 def candidates(game: String, pos: Pos): List[Char] = 
       
   446   allValues.diff(toAvoid(game, pos))
       
   447 
       
   448 //candidates(game0, (0,0))
       
   449 
       
   450 def pretty(game: String): String = 
       
   451   "\n" + (game.sliding(MaxValue, MaxValue).mkString("\n"))
       
   452 
       
   453 
       
   454 def search(game: String): List[String] = {
       
   455   if (isDone(game)) List(game)
       
   456   else {
       
   457     val cs = candidates(game, emptyPosition(game))
       
   458     cs.map(c => search(update(game, empty(game), c))).toList.flatten
       
   459   }
       
   460 }
       
   461 
       
   462 search(game0).map(pretty)
       
   463 
       
   464 val game1 = """23.915...
       
   465               |...2..54.
       
   466               |6.7......
       
   467               |..1.....9
       
   468               |89.5.3.17
       
   469               |5.....6..
       
   470               |......9.5
       
   471               |.16..7...
       
   472               |...329..1""".stripMargin.replaceAll("\\n", "")
       
   473 
       
   474 
       
   475 // game that is in the hard category
       
   476 val game2 = """8........
       
   477               |..36.....
       
   478               |.7..9.2..
       
   479               |.5...7...
       
   480               |....457..
       
   481               |...1...3.
       
   482               |..1....68
       
   483               |..85...1.
       
   484               |.9....4..""".stripMargin.replaceAll("\\n", "")
       
   485 
   376 
   486 // game with multiple solutions
   377 // game with multiple solutions
   487 val game3 = """.8...9743
   378 val game3 = """.8...9743
   488               |.5...8.1.
   379               |.5...8.1.
   489               |.1.......
   380               |.1.......
   492               |...3....6
   383               |...3....6
   493               |.......7.
   384               |.......7.
   494               |.3.5...8.
   385               |.3.5...8.
   495               |9724...5.""".stripMargin.replaceAll("\\n", "")
   386               |9724...5.""".stripMargin.replaceAll("\\n", "")
   496 
   387 
   497 
       
   498 search(game1).map(pretty)
       
   499 search(game3).map(pretty)
       
   500 search(game2).map(pretty)
       
   501 
       
   502 // for measuring time
       
   503 def time_needed[T](i: Int, code: => T) = {
       
   504   val start = System.nanoTime()
       
   505   for (j <- 1 to i) code
       
   506   val end = System.nanoTime()
       
   507   ((end - start) / 1.0e9) + " secs"
       
   508 }
       
   509 
       
   510 time_needed(1, search(game2))
       
   511 
       
   512 
       
   513 
       
   514 
       
   515 // Tail recursion
       
   516 //================
       
   517 
       
   518 
       
   519 def fact(n: Long): Long = 
       
   520   if (n == 0) 1 else n * fact(n - 1)
       
   521 
       
   522 def factB(n: BigInt): BigInt = 
       
   523   if (n == 0) 1 else n * factB(n - 1)
       
   524 
       
   525 factB(100000)
       
   526 
       
   527 fact(10)              //ok
       
   528 fact(10000)           // produces a stackoverflow
       
   529 
       
   530 def factT(n: BigInt, acc: BigInt): BigInt =
       
   531   if (n == 0) acc else factT(n - 1, n * acc)
       
   532 
       
   533 factT(10, 1)
       
   534 println(factT(100000, 1))
       
   535 
       
   536 // there is a flag for ensuring a function is tail recursive
       
   537 import scala.annotation.tailrec
       
   538 
       
   539 @tailrec
       
   540 def factT(n: BigInt, acc: BigInt): BigInt =
       
   541   if (n == 0) acc else factT(n - 1, n * acc)
       
   542 
       
   543 
       
   544 
       
   545 // for tail-recursive functions the Scala compiler
       
   546 // generates loop-like code, which does not need
       
   547 // to allocate stack-space in each recursive
       
   548 // call; Scala can do this only for tail-recursive
       
   549 // functions
       
   550 
       
   551 // tail recursive version that searches 
       
   552 // for all solutions
       
   553 
       
   554 def searchT(games: List[String], sols: List[String]): List[String] = games match {
       
   555   case Nil => sols
       
   556   case game::rest => {
       
   557     if (isDone(game)) searchT(rest, game::sols)
       
   558     else {
       
   559       val cs = candidates(game, emptyPosition(game))
       
   560       searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols)
       
   561     }
       
   562   }
       
   563 }
       
   564 
       
   565 searchT(List(game3), List()).map(pretty)
       
   566 
       
   567 
       
   568 // tail recursive version that searches 
       
   569 // for a single solution
       
   570 
       
   571 def search1T(games: List[String]): Option[String] = games match {
       
   572   case Nil => None
       
   573   case game::rest => {
       
   574     if (isDone(game)) Some(game)
       
   575     else {
       
   576       val cs = candidates(game, emptyPosition(game))
       
   577       search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
       
   578     }
       
   579   }
       
   580 }
       
   581 
       
   582 search1T(List(game3)).map(pretty)
       
   583 time_needed(10, search1T(List(game3)))
       
   584 
       
   585 
       
   586 // game with multiple solutions
       
   587 val game3 = """.8...9743
       
   588               |.5...8.1.
       
   589               |.1.......
       
   590               |8....5...
       
   591               |...8.4...
       
   592               |...3....6
       
   593               |.......7.
       
   594               |.3.5...8.
       
   595               |9724...5.""".stripMargin.replaceAll("\\n", "")
       
   596 
       
   597 searchT(List(game3), Nil).map(pretty)
   388 searchT(List(game3), Nil).map(pretty)
   598 search1T(List(game3)).map(pretty)
   389 search1T(List(game3)).map(pretty)
   599 
   390 
   600 // Moral: Whenever a recursive function is resource-critical
   391 // Moral: Whenever a recursive function is resource-critical
   601 // (i.e. works with large recursion depth), then you need to
   392 // (i.e. works with large recursion depth), then you need to