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