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