progs/lecture4.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 30 Nov 2020 03:33:30 +0000
changeset 380 d19b0a50ceb9
parent 326 e5453add7df6
child 381 116fa3c8584f
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
380
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
    91
// Polymorphic Types
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
    92
//===================
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
    93
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
    94
// You do not want to write functions like contains, first, 
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
    95
// length and so on for every type of lists.
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
    96
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
    97
def length_int_list(lst: List[Int]): Int = lst match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
    98
  case Nil => 0
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
    99
  case x::xs => 1 + length_int_list(xs)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   100
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   101
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   102
length_int_list(List(1, 2, 3, 4))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   103
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   104
def length_string_list(lst: List[String]): Int = lst match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   105
  case Nil => 0
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   106
  case _::xs => 1 + length_string_list(xs)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   107
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   108
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   109
length_string_list(List("1", "2", "3", "4"))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   110
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   111
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   112
// you can make the function parametric in type(s)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   113
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   114
def length[A](lst: List[A]): Int = lst match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   115
  case Nil => 0
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   116
  case x::xs => 1 + length(xs)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   117
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   118
length(List("1", "2", "3", "4"))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   119
length(List(1, 2, 3, 4))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   120
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   121
length[Int](List(1, 2, 3, 4))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   122
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   123
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   124
def map[A, B](lst: List[A], f: A => B): List[B] = lst match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   125
  case Nil => Nil
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   126
  case x::xs => f(x)::map(xs, f) 
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   127
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   128
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   129
map(List(1, 2, 3, 4), (x: Int) => x.toString)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   130
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   131
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   132
// from knight1.scala
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   133
def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = ???
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   134
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   135
// should be
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   136
def first[A, B](xs: List[A], f: A => Option[B]) : Option[B] = ???
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   137
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   138
// Type inference is local in Scala
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   139
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   140
def id[T](x: T) : T = x
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   141
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   142
val x = id(322)          // Int
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   143
val y = id("hey")        // String
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   144
val z = id(Set(1,2,3,4)) // Set[Int]
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   145
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   146
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   147
// The type variable concept in Scala can get really complicated.
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   148
//
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   149
// - variance (OO)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   150
// - bounds (subtyping)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   151
// - quantification
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   152
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   153
// Java has issues with this too: Java allows
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   154
// to write the following incorrect code, and
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   155
// only recovers by raising an exception
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   156
// at runtime.
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   157
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   158
// Object[] arr = new Integer[10];
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   159
// arr[0] = "Hello World";
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   160
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   161
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   162
// Scala gives you a compile-time error, which
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   163
// is much better.
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   164
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   165
var arr = Array[Int]()
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   166
arr(0) = "Hello World"
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   167
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   168
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   169
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   170
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   171
// Function definitions again
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   172
//============================
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   173
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   174
// variable arguments
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   175
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   176
def printAll(strings: String*) = {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   177
  strings.foreach(println)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   178
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   179
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   180
printAll()
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   181
printAll("foo")
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   182
printAll("foo", "bar")
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   183
printAll("foo", "bar", "baz")
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   184
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   185
// pass a list to the varargs field
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   186
val fruits = List("apple", "banana", "cherry")
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   187
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   188
printAll(fruits: _*)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   189
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   190
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   191
// you can also implement your own string interpolations
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   192
import scala.language.implicitConversions
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   193
import scala.language.reflectiveCalls
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   194
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   195
implicit def sring_inters(sc: StringContext) = new {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   196
    def i(args: Any*): String = s"${sc.s(args:_*)}\n"
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   197
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   198
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   199
i"add ${3+2} ${3 * 3}" 
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   200
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   201
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   202
// default arguments
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   203
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   204
def length[A](xs: List[A]) : Int = xs match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   205
  case Nil => 0
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   206
  case _ :: tail => 1 + length(tail)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   207
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   208
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   209
def lengthT[A](xs: List[A], acc : Int = 0) : Int = xs match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   210
  case Nil => acc
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   211
  case _ :: tail => lengthT(tail, 1 + acc)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   212
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   213
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   214
lengthT(List.fill(100000)(1))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   215
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   216
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   217
def fact(n: BigInt, acc: BigInt = 1): BigInt =
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   218
  if (n == 0) acc else fact(n - 1, n * acc)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   219
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   220
fact(10)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   221
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   222
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   223
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   224
// currying    (Haskell Curry)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   225
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   226
def add(x: Int, y: Int) = x + y
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   227
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   228
List(1,2,3,4,5).map(x => add(3, x))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   229
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   230
def add2(x: Int)(y: Int) = x + y
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   231
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   232
List(1,2,3,4,5).map(add2(3))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   233
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   234
val a3 : Int => Int = add2(3)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   235
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   236
// currying helps sometimes with type inference
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   237
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   238
def find[A](xs: List[A])(pred: A => Boolean): Option[A] = {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   239
  xs match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   240
    case Nil => None
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   241
    case hd :: tl =>
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   242
      if (pred(hd)) Some(hd) else find(tl)(pred)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   243
  }
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   244
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   245
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   246
find(List(1, 2, 3))(x => x % 2 == 0)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   247
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   248
// Source.fromURL(url)(encoding)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   249
// Source.fromFile(name)(encoding)
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   250
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   251
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   252
// Sudoku 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   253
//========
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   254
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   255
// THE POINT OF THIS CODE IS NOT TO BE SUPER
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   256
// EFFICIENT AND FAST, just explaining exhaustive
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   257
// depth-first search
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
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   260
val game0 = """.14.6.3..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   261
              |62...4..9
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   262
              |.8..5.6..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   263
              |.6.2....3
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   264
              |.7..1..5.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   265
              |5....9.6.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   266
              |..6.2..3.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   267
              |1..5...92
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   268
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   269
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   270
candidates(game0, (0, 0))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   271
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   272
type Pos = (Int, Int)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   273
val EmptyValue = '.'
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   274
val MaxValue = 9
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   275
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   276
val allValues = "123456789".toList
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   277
val indexes = (0 to 8).toList
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   278
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   279
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   280
def empty(game: String) = game.indexOf(EmptyValue)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   281
def isDone(game: String) = empty(game) == -1 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   282
def emptyPosition(game: String) = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   283
  (empty(game) % MaxValue, empty(game) / MaxValue)
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
def get_row(game: String, y: Int) = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   287
  indexes.map(col => game(y * MaxValue + col))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   288
def get_col(game: String, x: Int) = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   289
  indexes.map(row => game(x + row * MaxValue))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   290
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   291
get_row(game0, 0)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   292
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   293
def get_box(game: String, pos: Pos): List[Char] = {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   294
    def base(p: Int): Int = (p / 3) * 3
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   295
    val x0 = base(pos._1)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   296
    val y0 = base(pos._2)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   297
    val ys = (y0 until y0 + 3).toList
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   298
    (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
   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
//get_row(game0, 0)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   302
//get_row(game0, 1)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   303
//get_col(game0, 0)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   304
//get_box(game0, (3, 1))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   305
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
// this is not mutable!!
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   308
def update(game: String, pos: Int, value: Char): String = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   309
  game.updated(pos, value)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   310
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   311
def toAvoid(game: String, pos: Pos): List[Char] = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   312
  (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
   313
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   314
def candidates(game: String, pos: Pos): List[Char] = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   315
  allValues.diff(toAvoid(game, pos))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   316
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   317
//candidates(game0, (0,0))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   318
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   319
def pretty(game: String): String = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   320
  "\n" + (game.sliding(MaxValue, MaxValue).mkString("\n"))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   321
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   322
def search(game: String): List[String] = {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   323
  if (isDone(game)) List(game)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   324
  else {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   325
    val cs = candidates(game, emptyPosition(game))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   326
    cs.map(c => search(update(game, empty(game), c))).toList.flatten
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   327
  }
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   328
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   329
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   330
List(List("sol1"), List("sol2", "sol3")).flatten
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   331
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   332
search(game0).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   333
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   334
val game1 = """23.915...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   335
              |...2..54.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   336
              |6.7......
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   337
              |..1.....9
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   338
              |89.5.3.17
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   339
              |5.....6..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   340
              |......9.5
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   341
              |.16..7...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   342
              |...329..1""".stripMargin.replaceAll("\\n", "")
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
search(game1).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   345
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   346
// a game that is in the hard category
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   347
val game2 = """8........
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   348
              |..36.....
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   349
              |.7..9.2..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   350
              |.5...7...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   351
              |....457..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   352
              |...1...3.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   353
              |..1....68
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   354
              |..85...1.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   355
              |.9....4..""".stripMargin.replaceAll("\\n", "")
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
search(game2).map(pretty)
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
// game with multiple solutions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   360
val game3 = """.8...9743
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   361
              |.5...8.1.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   362
              |.1.......
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   363
              |8....5...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   364
              |...8.4...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   365
              |...3....6
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   366
              |.......7.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   367
              |.3.5...8.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   368
              |9724...5.""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   369
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   370
search(game3).map(pretty).foreach(println)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   371
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   372
// for measuring time
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   373
def time_needed[T](i: Int, code: => T) = {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   374
  val start = System.nanoTime()
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   375
  for (j <- 1 to i) code
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   376
  val end = System.nanoTime()
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   377
  s"${(end - start) / 1.0e9} secs"
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   378
}
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
time_needed(1, search(game2))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   381
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   382
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
// Tail recursion
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
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   387
@tailrec
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   388
def fact(n: BigInt): BigInt = 
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   389
  if (n == 0) 1 else n * fact(n - 1)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   390
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   391
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   392
fact(10)          
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   393
fact(1000)        
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   394
fact(100000)       
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   395
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   396
def factB(n: BigInt): BigInt = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   397
  if (n == 0) 1 else n * factB(n - 1)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   398
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   399
def factT(n: BigInt, acc: BigInt): BigInt =
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   400
  if (n == 0) acc else factT(n - 1, n * acc)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   401
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   402
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   403
factB(1000)
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
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   406
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   407
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   408
factT(10, 1)
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   409
println(factT(500000, 1))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   410
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   411
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   412
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   413
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   414
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   415
// there is a flag for ensuring a function is tail recursive
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   416
import scala.annotation.tailrec
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   417
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   418
@tailrec
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   419
def factT(n: BigInt, acc: BigInt): BigInt =
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   420
  if (n == 0) acc else factT(n - 1, n * acc)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   421
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   422
factT(100000, 1)
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
// for tail-recursive functions the Scala compiler
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   425
// generates loop-like code, which does not need
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   426
// to allocate stack-space in each recursive
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   427
// call; Scala can do this only for tail-recursive
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   428
// functions
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
// tail recursive version that searches 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   431
// for all Sudoku solutions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   432
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   433
@tailrec
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   434
def searchT(games: List[String], sols: List[String]): List[String] = games match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   435
  case Nil => sols
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   436
  case game::rest => {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   437
    if (isDone(game)) searchT(rest, game::sols)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   438
    else {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   439
      val cs = candidates(game, emptyPosition(game))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   440
      searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   441
    }
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   442
  }
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
searchT(List(game3), List()).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   446
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   447
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   448
// tail recursive version that searches 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   449
// for a single solution
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   450
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   451
def search1T(games: List[String]): Option[String] = games match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   452
  case Nil => None
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   453
  case game::rest => {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   454
    if (isDone(game)) Some(game)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   455
    else {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   456
      val cs = candidates(game, emptyPosition(game))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   457
      search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   458
    }
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   459
  }
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   460
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   461
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   462
search1T(List(game3)).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   463
time_needed(1, search1T(List(game3)))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   464
time_needed(1, search1T(List(game2)))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   465
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   466
// game with multiple solutions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   467
val game3 = """.8...9743
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   468
              |.5...8.1.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   469
              |.1.......
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   470
              |8....5...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   471
              |...8.4...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   472
              |...3....6
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   473
              |.......7.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   474
              |.3.5...8.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   475
              |9724...5.""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   476
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   477
searchT(List(game3), Nil).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   478
search1T(List(game3)).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   479
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   480
// Moral: Whenever a recursive function is resource-critical
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   481
// (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
   482
// write it in tail-recursive fashion.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   483
// 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   484
// Unfortuantely, Scala because of current limitations in 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   485
// the JVM is not as clever as other functional languages. It can 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   486
// only optimise "self-tail calls". This excludes the cases of 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   487
// multiple functions making tail calls to each other. Well,
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   488
// nothing is perfect. 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   489
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   490
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   491
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   492
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   493
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   494
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   495
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   496
// Cool Stuff in Scala
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   497
//=====================
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   498
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   499
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   500
// Implicits or How to Pimp your Library
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   501
//======================================
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   502
//
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   503
// For example adding your own methods to Strings:
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   504
// Imagine you want to increment strings, like
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   505
//
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   506
//     "HAL".increment
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   507
//
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   508
// you can avoid ugly fudges, like a MyString, by
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   509
// using implicit conversions.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   510
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   511
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   512
implicit class MyString(s: String) {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   513
  def increment = s.map(c => (c + 1).toChar) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   514
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   515
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   516
"HAL".increment
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   517
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   518
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   519
// Abstract idea:
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   520
// In that version implicit conversions were used to solve the 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   521
// late extension problem; namely, given a class C and a class T, 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   522
// how to have C extend T without touching or recompiling C. 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   523
// Conversions add a wrapper when a member of T is requested 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   524
// from an instance of C.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   525
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   526
//Another example (TimeUnit in 2.13?)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   527
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   528
import scala.concurrent.duration.{TimeUnit,SECONDS,MINUTES}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   529
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   530
case class Duration(time: Long, unit: TimeUnit) {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   531
  def +(o: Duration) = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   532
    Duration(time + unit.convert(o.time, o.unit), unit)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   533
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   534
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   535
implicit class Int2Duration(that: Int) {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   536
  def seconds = new Duration(that, SECONDS)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   537
  def minutes = new Duration(that, MINUTES)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   538
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   539
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   540
5.seconds + 2.minutes   //Duration(125L, SECONDS )
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   541
2.minutes + 60.seconds
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   542
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   543
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   544
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   545
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   546
// Regular expressions - the power of DSLs in Scala
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   547
//==================================================
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   548
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   549
abstract class Rexp
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   550
case object ZERO extends Rexp                     // nothing
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   551
case object ONE extends Rexp                      // the empty string
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   552
case class CHAR(c: Char) extends Rexp             // a character c
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   553
case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative  r1 + r2
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   554
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence     r1 . r2  
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   555
case class STAR(r: Rexp) extends Rexp             // star         r*
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   556
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   557
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   558
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   559
// writing (ab)* in the format above is 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   560
// tedious
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   561
val r0 = STAR(SEQ(CHAR('a'), CHAR('b')))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   562
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   563
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   564
// some convenience for typing in regular expressions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   565
import scala.language.implicitConversions    
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   566
import scala.language.reflectiveCalls 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   567
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   568
def charlist2rexp(s: List[Char]): Rexp = s match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   569
  case Nil => ONE
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   570
  case c::Nil => CHAR(c)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   571
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   572
}
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   573
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   574
implicit def string2rexp(s: String): Rexp = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   575
  charlist2rexp(s.toList)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   576
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   577
"(a|b)"
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   578
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   579
val r1 = STAR("ab")
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   580
val r2 = (STAR("ab")) | (STAR("ba"))
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   581
val r3 = STAR(SEQ("ab", ALT("a", "b")))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   582
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   583
implicit def RexpOps (r: Rexp) = new {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   584
  def | (s: Rexp) = ALT(r, s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   585
  def % = STAR(r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   586
  def ~ (s: Rexp) = SEQ(r, s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   587
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   588
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   589
implicit def stringOps (s: String) = new {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   590
  def | (r: Rexp) = ALT(s, r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   591
  def | (r: String) = ALT(s, r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   592
  def % = STAR(s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   593
  def ~ (r: Rexp) = SEQ(s, r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   594
  def ~ (r: String) = SEQ(s, r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   595
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   596
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   597
//example regular expressions
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   598
val digit = ("0" | "1" | "2" | "3" | "4" | 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   599
              "5" | "6" | "7" | "8" | "9")
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   600
val sign = "+" | "-" | ""
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   601
val number = sign ~ digit ~ digit.% 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   602
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   603
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   604
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   605
// Mind-Blowing Regular Expressions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   606
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   607
// same examples using the internal regexes
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   608
val evil = "(a*)*b"
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   609
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   610
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   611
println("a" * 100)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   612
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   613
("a" * 10000).matches(evil)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   614
("a" * 10).matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   615
("a" * 10000).matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   616
("a" * 20000).matches(evil)
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   617
("a" * 50000).matches(evil)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   618
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   619
time_needed(1, ("a" * 50000).matches(evil))