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