progs/lecture4.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 09 Nov 2020 14:34:37 +0000
changeset 357 826a8dbb658c
parent 326 e5453add7df6
child 380 d19b0a50ceb9
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     1
// Scala Lecture 4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     2
//=================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     3
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     4
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
     5
// expressions (essentially trees)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
     6
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
     7
abstract class Exp
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
     8
case class N(n: Int) extends Exp                  // for numbers
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
     9
case class Plus(e1: Exp, e2: Exp) extends Exp
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    10
case class Times(e1: Exp, e2: Exp) extends Exp
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    11
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    12
def string(e: Exp) : String = e match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    13
  case N(n) => s"$n"
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    14
  case Plus(e1, e2) => s"(${string(e1)} + ${string(e2)})" 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    15
  case Times(e1, e2) => s"(${string(e1)} * ${string(e2)})"
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    16
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    17
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    18
val e = Plus(N(9), Times(N(3), N(4)))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    19
println(string(e))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    20
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    21
def eval(e: Exp) : Int = e match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    22
  case N(n) => n
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    23
  case Plus(e1, e2) => eval(e1) + eval(e2) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    24
  case Times(e1, e2) => eval(e1) * eval(e2) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    25
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    26
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    27
println(eval(e))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    28
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    29
// simplification rules:
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    30
// e + 0, 0 + e => e 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    31
// e * 0, 0 * e => 0
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    32
// e * 1, 1 * e => e
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
    33
//
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
    34
// (....0  ....)
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    35
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    36
def simp(e: Exp) : Exp = e match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    37
  case N(n) => N(n)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    38
  case Plus(e1, e2) => (simp(e1), simp(e2)) match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    39
    case (N(0), e2s) => e2s
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    40
    case (e1s, N(0)) => e1s
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    41
    case (e1s, e2s) => Plus(e1s, e2s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    42
  }  
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    43
  case Times(e1, e2) => (simp(e1), simp(e2)) match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    44
    case (N(0), _) => N(0)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    45
    case (_, N(0)) => N(0)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    46
    case (N(1), e2s) => e2s
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    47
    case (e1s, N(1)) => e1s
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    48
    case (e1s, e2s) => Times(e1s, e2s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    49
  }  
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    50
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    51
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    52
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    53
val e2 = Times(Plus(N(0), N(1)), Plus(N(0), N(9)))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    54
println(string(e2))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    55
println(string(simp(e2)))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    56
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    57
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    58
// Tokens and Reverse Polish Notation
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    59
abstract class Token
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    60
case class T(n: Int) extends Token
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    61
case object PL extends Token
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    62
case object TI extends Token
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    63
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    64
// transfroming an Exp into a list of tokens
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    65
def rp(e: Exp) : List[Token] = e match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    66
  case N(n) => List(T(n))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    67
  case Plus(e1, e2) => rp(e1) ::: rp(e2) ::: List(PL) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    68
  case Times(e1, e2) => rp(e1) ::: rp(e2) ::: List(TI) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    69
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    70
println(string(e2))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    71
println(rp(e2))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    72
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
    73
def comp(ls: List[Token], st: List[Int] = Nil) : Int = (ls, st) match {
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    74
  case (Nil, st) => st.head 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    75
  case (T(n)::rest, st) => comp(rest, n::st)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    76
  case (PL::rest, n1::n2::st) => comp(rest, n1 + n2::st)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    77
  case (TI::rest, n1::n2::st) => comp(rest, n1 * n2::st)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    78
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    79
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
    80
comp(rp(e))
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    81
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    82
def proc(s: String) : Token = s match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    83
  case  "+" => PL
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    84
  case  "*" => TI
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    85
  case  _ => T(s.toInt) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    86
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    87
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    88
comp("1 2 + 4 * 5 + 3 +".split(" ").toList.map(proc), Nil)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    89
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    90
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    91
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    92
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    93
// Sudoku 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    94
//========
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    95
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    96
// THE POINT OF THIS CODE IS NOT TO BE SUPER
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    97
// EFFICIENT AND FAST, just explaining exhaustive
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    98
// depth-first search
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    99
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   100
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   101
val game0 = """.14.6.3..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   102
              |62...4..9
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   103
              |.8..5.6..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   104
              |.6.2....3
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   105
              |.7..1..5.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   106
              |5....9.6.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   107
              |..6.2..3.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   108
              |1..5...92
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   109
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   110
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   111
candidates(game0, (0, 0))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   112
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   113
type Pos = (Int, Int)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   114
val EmptyValue = '.'
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   115
val MaxValue = 9
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   116
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   117
val allValues = "123456789".toList
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   118
val indexes = (0 to 8).toList
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   119
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   120
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   121
def empty(game: String) = game.indexOf(EmptyValue)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   122
def isDone(game: String) = empty(game) == -1 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   123
def emptyPosition(game: String) = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   124
  (empty(game) % MaxValue, empty(game) / MaxValue)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   125
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   126
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   127
def get_row(game: String, y: Int) = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   128
  indexes.map(col => game(y * MaxValue + col))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   129
def get_col(game: String, x: Int) = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   130
  indexes.map(row => game(x + row * MaxValue))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   131
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   132
get_row(game0, 0)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   133
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   134
def get_box(game: String, pos: Pos): List[Char] = {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   135
    def base(p: Int): Int = (p / 3) * 3
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   136
    val x0 = base(pos._1)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   137
    val y0 = base(pos._2)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   138
    val ys = (y0 until y0 + 3).toList
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   139
    (x0 until x0 + 3).toList.flatMap(x => ys.map(y => game(x + y * MaxValue)))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   140
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   141
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   142
//get_row(game0, 0)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   143
//get_row(game0, 1)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   144
//get_col(game0, 0)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   145
//get_box(game0, (3, 1))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   146
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   147
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   148
// this is not mutable!!
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   149
def update(game: String, pos: Int, value: Char): String = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   150
  game.updated(pos, value)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   151
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   152
def toAvoid(game: String, pos: Pos): List[Char] = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   153
  (get_col(game, pos._1) ++ get_row(game, pos._2) ++ get_box(game, pos))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   154
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   155
def candidates(game: String, pos: Pos): List[Char] = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   156
  allValues.diff(toAvoid(game, pos))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   157
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   158
//candidates(game0, (0,0))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   159
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   160
def pretty(game: String): String = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   161
  "\n" + (game.sliding(MaxValue, MaxValue).mkString("\n"))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   162
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   163
def search(game: String): List[String] = {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   164
  if (isDone(game)) List(game)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   165
  else {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   166
    val cs = candidates(game, emptyPosition(game))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   167
    cs.map(c => search(update(game, empty(game), c))).toList.flatten
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   168
  }
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   169
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   170
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   171
List(List("sol1"), List("sol2", "sol3")).flatten
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   172
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   173
search(game0).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   174
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   175
val game1 = """23.915...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   176
              |...2..54.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   177
              |6.7......
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   178
              |..1.....9
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   179
              |89.5.3.17
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   180
              |5.....6..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   181
              |......9.5
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   182
              |.16..7...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   183
              |...329..1""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   184
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   185
search(game1).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   186
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   187
// a game that is in the hard category
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   188
val game2 = """8........
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   189
              |..36.....
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   190
              |.7..9.2..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   191
              |.5...7...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   192
              |....457..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   193
              |...1...3.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   194
              |..1....68
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   195
              |..85...1.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   196
              |.9....4..""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   197
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   198
search(game2).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   199
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   200
// game with multiple solutions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   201
val game3 = """.8...9743
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   202
              |.5...8.1.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   203
              |.1.......
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   204
              |8....5...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   205
              |...8.4...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   206
              |...3....6
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   207
              |.......7.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   208
              |.3.5...8.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   209
              |9724...5.""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   210
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   211
search(game3).map(pretty).foreach(println)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   212
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   213
// for measuring time
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   214
def time_needed[T](i: Int, code: => T) = {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   215
  val start = System.nanoTime()
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   216
  for (j <- 1 to i) code
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   217
  val end = System.nanoTime()
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   218
  s"${(end - start) / 1.0e9} secs"
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   219
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   220
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   221
time_needed(1, search(game2))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   222
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   223
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   224
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   225
// Tail recursion
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   226
//================
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   227
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   228
@tailrec
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   229
def fact(n: BigInt): BigInt = 
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   230
  if (n == 0) 1 else n * fact(n - 1)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   231
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   232
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   233
fact(10)          
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   234
fact(1000)        
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   235
fact(100000)       
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   236
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   237
def factB(n: BigInt): BigInt = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   238
  if (n == 0) 1 else n * factB(n - 1)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   239
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   240
def factT(n: BigInt, acc: BigInt): BigInt =
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   241
  if (n == 0) acc else factT(n - 1, n * acc)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   242
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   243
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   244
factB(1000)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   245
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   246
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   247
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   248
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   249
factT(10, 1)
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   250
println(factT(500000, 1))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   251
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   252
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   253
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   254
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   255
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   256
// there is a flag for ensuring a function is tail recursive
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   257
import scala.annotation.tailrec
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   258
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   259
@tailrec
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   260
def factT(n: BigInt, acc: BigInt): BigInt =
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   261
  if (n == 0) acc else factT(n - 1, n * acc)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   262
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   263
factT(100000, 1)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   264
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   265
// for tail-recursive functions the Scala compiler
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   266
// generates loop-like code, which does not need
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   267
// to allocate stack-space in each recursive
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   268
// call; Scala can do this only for tail-recursive
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   269
// functions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   270
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   271
// tail recursive version that searches 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   272
// for all Sudoku solutions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   273
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   274
@tailrec
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   275
def searchT(games: List[String], sols: List[String]): List[String] = games match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   276
  case Nil => sols
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   277
  case game::rest => {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   278
    if (isDone(game)) searchT(rest, game::sols)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   279
    else {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   280
      val cs = candidates(game, emptyPosition(game))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   281
      searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   282
    }
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   283
  }
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   284
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   285
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   286
searchT(List(game3), List()).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   287
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   288
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   289
// tail recursive version that searches 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   290
// for a single solution
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   291
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   292
def search1T(games: List[String]): Option[String] = games match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   293
  case Nil => None
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   294
  case game::rest => {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   295
    if (isDone(game)) Some(game)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   296
    else {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   297
      val cs = candidates(game, emptyPosition(game))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   298
      search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   299
    }
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   300
  }
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   301
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   302
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   303
search1T(List(game3)).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   304
time_needed(1, search1T(List(game3)))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   305
time_needed(1, search1T(List(game2)))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   306
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   307
// game with multiple solutions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   308
val game3 = """.8...9743
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   309
              |.5...8.1.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   310
              |.1.......
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   311
              |8....5...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   312
              |...8.4...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   313
              |...3....6
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   314
              |.......7.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   315
              |.3.5...8.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   316
              |9724...5.""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   317
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   318
searchT(List(game3), Nil).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   319
search1T(List(game3)).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   320
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   321
// Moral: Whenever a recursive function is resource-critical
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   322
// (i.e. works with a large recursion depth), then you need to
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   323
// write it in tail-recursive fashion.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   324
// 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   325
// Unfortuantely, Scala because of current limitations in 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   326
// the JVM is not as clever as other functional languages. It can 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   327
// only optimise "self-tail calls". This excludes the cases of 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   328
// multiple functions making tail calls to each other. Well,
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   329
// nothing is perfect. 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   330
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   331
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   332
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   333
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   334
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   335
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   336
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   337
// Cool Stuff in Scala
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   338
//=====================
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   339
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   340
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   341
// Implicits or How to Pimp your Library
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   342
//======================================
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   343
//
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   344
// For example adding your own methods to Strings:
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   345
// Imagine you want to increment strings, like
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   346
//
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   347
//     "HAL".increment
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   348
//
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   349
// you can avoid ugly fudges, like a MyString, by
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   350
// using implicit conversions.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   351
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   352
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   353
implicit class MyString(s: String) {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   354
  def increment = s.map(c => (c + 1).toChar) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   355
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   356
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   357
"HAL".increment
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   358
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   359
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   360
// Abstract idea:
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   361
// In that version implicit conversions were used to solve the 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   362
// late extension problem; namely, given a class C and a class T, 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   363
// how to have C extend T without touching or recompiling C. 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   364
// Conversions add a wrapper when a member of T is requested 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   365
// from an instance of C.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   366
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   367
//Another example (TimeUnit in 2.13?)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   368
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   369
import scala.concurrent.duration.{TimeUnit,SECONDS,MINUTES}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   370
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   371
case class Duration(time: Long, unit: TimeUnit) {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   372
  def +(o: Duration) = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   373
    Duration(time + unit.convert(o.time, o.unit), unit)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   374
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   375
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   376
implicit class Int2Duration(that: Int) {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   377
  def seconds = new Duration(that, SECONDS)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   378
  def minutes = new Duration(that, MINUTES)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   379
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   380
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   381
5.seconds + 2.minutes   //Duration(125L, SECONDS )
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   382
2.minutes + 60.seconds
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   383
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   384
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   385
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   386
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   387
// Regular expressions - the power of DSLs in Scala
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   388
//==================================================
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   389
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   390
abstract class Rexp
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   391
case object ZERO extends Rexp                     // nothing
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   392
case object ONE extends Rexp                      // the empty string
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   393
case class CHAR(c: Char) extends Rexp             // a character c
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   394
case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative  r1 + r2
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   395
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence     r1 . r2  
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   396
case class STAR(r: Rexp) extends Rexp             // star         r*
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   397
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   398
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   399
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   400
// writing (ab)* in the format above is 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   401
// tedious
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   402
val r0 = STAR(SEQ(CHAR('a'), CHAR('b')))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   403
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   404
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   405
// some convenience for typing in regular expressions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   406
import scala.language.implicitConversions    
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   407
import scala.language.reflectiveCalls 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   408
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   409
def charlist2rexp(s: List[Char]): Rexp = s match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   410
  case Nil => ONE
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   411
  case c::Nil => CHAR(c)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   412
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   413
}
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   414
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   415
implicit def string2rexp(s: String): Rexp = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   416
  charlist2rexp(s.toList)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   417
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   418
"(a|b)"
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   419
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   420
val r1 = STAR("ab")
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   421
val r2 = (STAR("ab")) | (STAR("ba"))
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   422
val r3 = STAR(SEQ("ab", ALT("a", "b")))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   423
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   424
implicit def RexpOps (r: Rexp) = new {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   425
  def | (s: Rexp) = ALT(r, s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   426
  def % = STAR(r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   427
  def ~ (s: Rexp) = SEQ(r, s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   428
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   429
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   430
implicit def stringOps (s: String) = new {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   431
  def | (r: Rexp) = ALT(s, r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   432
  def | (r: String) = ALT(s, r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   433
  def % = STAR(s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   434
  def ~ (r: Rexp) = SEQ(s, r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   435
  def ~ (r: String) = SEQ(s, r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   436
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   437
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   438
//example regular expressions
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   439
val digit = ("0" | "1" | "2" | "3" | "4" | 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   440
              "5" | "6" | "7" | "8" | "9")
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   441
val sign = "+" | "-" | ""
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   442
val number = sign ~ digit ~ digit.% 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   443
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   444
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   445
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   446
// Mind-Blowing Regular Expressions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   447
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   448
// same examples using the internal regexes
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   449
val evil = "(a*)*b"
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   450
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   451
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   452
println("a" * 100)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   453
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   454
("a" * 10000).matches(evil)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   455
("a" * 10).matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   456
("a" * 10000).matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   457
("a" * 20000).matches(evil)
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   458
("a" * 50000).matches(evil)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   459
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   460
time_needed(1, ("a" * 50000).matches(evil))