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