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