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