progs/lecture3.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 24 Nov 2020 09:04:06 +0000
changeset 366 1c829680503e
parent 364 f1a6fa599d26
child 374 90b267768329
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     1
// Scala Lecture 3
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     2
//=================
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     3
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
     4
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
     5
// naive quicksort with "On" function
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
     6
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
     7
def sortOn(f: Int => Int, xs: List[Int]) : List[Int] = {
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
     8
  if (xs.size < 2) xs
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
     9
  else {
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    10
   val pivot = xs.head
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    11
   val (left, right) = xs.partition(f(_) < f(pivot))
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    12
   sortOn(f, left) ::: pivot :: sortOn(f, right.tail)
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    13
  }
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    14
} 
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    15
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    16
sortOn(identity, List(99,99,99,98,10,-3,2)) 
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    17
sortOn(n => - n, List(99,99,99,98,10,-3,2))
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    18
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    19
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    20
// Recursion Again ;o)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    21
//====================
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    22
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    23
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
    24
// another well-known example: Towers of Hanoi
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
    25
//=============================================
178
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
    26
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    27
def move(from: Char, to: Char) =
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    28
  println(s"Move disc from $from to $to!")
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    29
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    30
def hanoi(n: Int, from: Char, via: Char, to: Char) : Unit = {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    31
  if (n == 0) ()
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    32
  else {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    33
    hanoi(n - 1, from, to, via)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    34
    move(from, to)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    35
    hanoi(n - 1, via, from, to)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    36
  }
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    37
} 
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    38
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    39
hanoi(4, 'A', 'B', 'C')
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    40
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    41
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    42
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    43
// User-defined Datatypes
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    44
//========================
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    45
323
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 321
diff changeset
    46
abstract class Tree
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 321
diff changeset
    47
case class Leaf(x: Int) extends Tree
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 321
diff changeset
    48
case class Node(s: String, left: Tree, right: Tree) extends Tree 
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 321
diff changeset
    49
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
    50
val lf = Leaf(20)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
    51
val tr = Node("foo", Leaf(10), Leaf(23))
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    52
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
    53
val lst : List[Tree] = List(lf, tr)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
    54
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
    55
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
    56
abstract class Colour
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    57
case object Red extends Colour 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    58
case object Green extends Colour 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    59
case object Blue extends Colour
323
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 321
diff changeset
    60
case object Yellow extends Colour
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    61
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    62
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    63
def fav_colour(c: Colour) : Boolean = c match {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    64
  case Green => true
323
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 321
diff changeset
    65
  case _  => false 
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    66
}
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    67
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
    68
fav_colour(Blue)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
    69
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    70
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    71
// ... a tiny bit more useful: Roman Numerals
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    72
321
7b0055205ec9 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    73
sealed abstract class RomanDigit 
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    74
case object I extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    75
case object V extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    76
case object X extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    77
case object L extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    78
case object C extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    79
case object D extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    80
case object M extends RomanDigit 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    81
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    82
type RomanNumeral = List[RomanDigit] 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    83
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
    84
List(X,I,M,A)
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    85
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    86
/*
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    87
I    -> 1
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    88
II   -> 2
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    89
III  -> 3
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    90
IV   -> 4
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    91
V    -> 5
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    92
VI   -> 6
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    93
VII  -> 7
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    94
VIII -> 8
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    95
IX   -> 9
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    96
X    -> 10
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    97
*/
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    98
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    99
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match { 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   100
  case Nil => 0
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   101
  case M::r    => 1000 + RomanNumeral2Int(r)  
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   102
  case C::M::r => 900 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   103
  case D::r    => 500 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   104
  case C::D::r => 400 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   105
  case C::r    => 100 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   106
  case X::C::r => 90 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   107
  case L::r    => 50 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   108
  case X::L::r => 40 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   109
  case X::r    => 10 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   110
  case I::X::r => 9 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   111
  case V::r    => 5 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   112
  case I::V::r => 4 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   113
  case I::r    => 1 + RomanNumeral2Int(r)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   114
}
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   115
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   116
RomanNumeral2Int(List(I,V))             // 4
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   117
RomanNumeral2Int(List(I,I,I,I))         // 4 (invalid Roman number)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   118
RomanNumeral2Int(List(V,I))             // 6
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   119
RomanNumeral2Int(List(I,X))             // 9
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   120
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   121
RomanNumeral2Int(List(M,M,X,V,I,I))     // 2017
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   122
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   123
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   124
// expressions (essentially trees)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   125
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   126
abstract class Exp
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   127
case class N(n: Int) extends Exp                  // for numbers
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   128
case class Plus(e1: Exp, e2: Exp) extends Exp
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   129
case class Times(e1: Exp, e2: Exp) extends Exp
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   130
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   131
def string(e: Exp) : String = e match {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   132
  case N(n) => s"$n"
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   133
  case Plus(e1, e2) => s"(${string(e1)} + ${string(e2)})" 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   134
  case Times(e1, e2) => s"(${string(e1)} * ${string(e2)})"
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   135
}
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   136
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   137
val e = Plus(N(9), Times(N(3), N(4)))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   138
e.toString
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   139
println(string(e))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   140
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   141
def eval(e: Exp) : Int = e match {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   142
  case N(n) => n
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   143
  case Plus(e1, e2) => eval(e1) + eval(e2) 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   144
  case Times(e1, e2) => eval(e1) * eval(e2) 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   145
}
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   146
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   147
println(eval(e))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   148
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   149
// simplification rules:
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   150
// e + 0, 0 + e => e 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   151
// e * 0, 0 * e => 0
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   152
// e * 1, 1 * e => e
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   153
//
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   154
// (....9 ....)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   155
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   156
def simp(e: Exp) : Exp = e match {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   157
  case N(n) => N(n)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   158
  case Plus(e1, e2) => (simp(e1), simp(e2)) match {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   159
    case (N(0), e2s) => e2s
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   160
    case (e1s, N(0)) => e1s
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   161
    case (e1s, e2s) => Plus(e1s, e2s)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   162
  }  
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   163
  case Times(e1, e2) => (simp(e1), simp(e2)) match {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   164
    case (N(0), _) => N(0)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   165
    case (_, N(0)) => N(0)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   166
    case (N(1), e2s) => e2s
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   167
    case (e1s, N(1)) => e1s
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   168
    case (e1s, e2s) => Times(e1s, e2s)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   169
  }  
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   170
}
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   171
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   172
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   173
val e2 = Times(Plus(N(0), N(1)), Plus(N(0), N(9)))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   174
println(string(e2))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   175
println(string(simp(e2)))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   176
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   177
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   178
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   179
// String interpolations as patterns
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   180
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   181
val date = "2019-11-26"
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   182
val s"$year-$month-$day" = date
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   183
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   184
def parse_date(date: String) : Option[(Int, Int, Int)]= date match {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   185
  case s"$year-$month-$day" => Some((day.toInt, month.toInt, year.toInt))
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   186
  case s"$day/$month/$year" => Some((day.toInt, month.toInt, year.toInt))
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   187
  case s"$day.$month.$year" => Some((day.toInt, month.toInt, year.toInt))
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   188
  case _ => None
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   189
} 
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   190
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   191
parse_date("2019-11-26")
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   192
parse_date("26/11/2019")
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   193
parse_date("26.11.2019")
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   194
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   195
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   196
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   197
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   198
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   199
// Tail recursion
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   200
//================
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   201
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   202
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   203
def fact(n: Long): Long = 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   204
  if (n == 0) 1 else n * fact(n - 1)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   205
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   206
def factB(n: BigInt): BigInt = 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   207
  if (n == 0) 1 else n * factB(n - 1)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   208
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   209
factB(100000)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   210
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   211
fact(10)              //ok
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   212
fact(10000)           // produces a stackoverflow
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   213
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   214
def factT(n: BigInt, acc: BigInt): BigInt =
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   215
  if (n == 0) acc else factT(n - 1, n * acc)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   216
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   217
factT(10, 1)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   218
println(factT(100000, 1))
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   219
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   220
// there is a flag for ensuring a function is tail recursive
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   221
import scala.annotation.tailrec
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   222
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   223
@tailrec
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   224
def factT(n: BigInt, acc: BigInt): BigInt =
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   225
  if (n == 0) acc else factT(n - 1, n * acc)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   226
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   227
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   228
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   229
// for tail-recursive functions the Scala compiler
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   230
// generates loop-like code, which does not need
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   231
// to allocate stack-space in each recursive
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   232
// call; Scala can do this only for tail-recursive
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   233
// functions
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   234
366
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   235
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   236
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   237
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   238
// Sudoku
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   239
//========
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   240
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   241
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   242
type Pos = (Int, Int)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   243
val emptyValue = '.'
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   244
val maxValue = 9
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   245
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   246
val allValues = "123456789".toList
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   247
val indexes = (0 to 8).toList
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   248
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   249
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   250
def empty(game: String) = game.indexOf(emptyValue)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   251
def isDone(game: String) = empty(game) == -1 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   252
def emptyPosition(game: String) : Pos = 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   253
  (empty(game) % maxValue, empty(game) / maxValue)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   254
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   255
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   256
def get_row(game: String, y: Int) = indexes.map(col => game(y * maxValue + col))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   257
def get_col(game: String, x: Int) = indexes.map(row => game(x + row * maxValue))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   258
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   259
def get_box(game: String, pos: Pos): List[Char] = {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   260
    def base(p: Int): Int = (p / 3) * 3
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   261
    val x0 = base(pos._1)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   262
    val y0 = base(pos._2)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   263
    for (x <- (x0 until x0 + 3).toList;
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   264
         y <- (y0 until y0 + 3).toList) yield game(x + y * maxValue)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   265
}         
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   266
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   267
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   268
def update(game: String, pos: Int, value: Char): String = 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   269
  game.updated(pos, value)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   270
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   271
def toAvoid(game: String, pos: Pos): List[Char] = 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   272
  (get_col(game, pos._1) ++ get_row(game, pos._2) ++ get_box(game, pos))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   273
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   274
def candidates(game: String, pos: Pos): List[Char] = 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   275
  allValues.diff(toAvoid(game, pos))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   276
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   277
def search(game: String): List[String] = {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   278
  if (isDone(game)) List(game)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   279
  else 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   280
    candidates(game, emptyPosition(game)).par.
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   281
      map(c => search(update(game, empty(game), c))).toList.flatten
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   282
}
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   283
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   284
def search1T(games: List[String]): Option[String] = games match {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   285
  case Nil => None
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   286
  case game::rest => {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   287
    if (isDone(game)) Some(game)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   288
    else {
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   289
      val cs = candidates(game, emptyPosition(game))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   290
      search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   291
    }
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   292
  }
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   293
}
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   294
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   295
def pretty(game: String): String = 
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   296
  "\n" + (game.sliding(maxValue, maxValue).mkString(",\n"))
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   297
1c829680503e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   298
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   299
// tail recursive version that searches 
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   300
// for all solutions
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   301
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   302
def searchT(games: List[String], sols: List[String]): List[String] = games match {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   303
  case Nil => sols
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   304
  case game::rest => {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   305
    if (isDone(game)) searchT(rest, game::sols)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   306
    else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   307
      val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   308
      searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   309
    }
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   310
  }
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   311
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   312
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   313
searchT(List(game3), List()).map(pretty)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   314
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   315
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   316
// tail recursive version that searches 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   317
// for a single solution
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   318
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   319
def search1T(games: List[String]): Option[String] = games match {
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   320
  case Nil => None
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   321
  case game::rest => {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   322
    if (isDone(game)) Some(game)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   323
    else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   324
      val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   325
      search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   326
    }
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   327
  }
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   328
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   329
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   330
search1T(List(game3)).map(pretty)
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   331
time_needed(10, search1T(List(game3)))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   332
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   333
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   334
// game with multiple solutions
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   335
val game3 = """.8...9743
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   336
              |.5...8.1.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   337
              |.1.......
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   338
              |8....5...
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   339
              |...8.4...
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   340
              |...3....6
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   341
              |.......7.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   342
              |.3.5...8.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   343
              |9724...5.""".stripMargin.replaceAll("\\n", "")
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   344
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   345
searchT(List(game3), Nil).map(pretty)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   346
search1T(List(game3)).map(pretty)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   347
77
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   348
// Moral: Whenever a recursive function is resource-critical
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   349
// (i.e. works with large recursion depth), then you need to
77
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   350
// write it in tail-recursive fashion.
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   351
// 
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   352
// Unfortuantely, Scala because of current limitations in 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   353
// the JVM is not as clever as other functional languages. It can 
77
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   354
// only optimise "self-tail calls". This excludes the cases of 
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   355
// multiple functions making tail calls to each other. Well,
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   356
// nothing is perfect. 
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   357