progs/lecture4.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 08 Dec 2023 00:54:36 +0000
changeset 481 e03a0100ec46
parent 455 557d18cce0f0
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     1
// Scala Lecture 4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     2
//=================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     3
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
     4
// pattern-matching
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     5
// tail-recursion
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
     6
// polymorphic types
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
     7
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
     8
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
     9
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    10
// Pattern Matching
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    11
//==================
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    12
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    13
// A powerful tool which has even landed in Java during 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    14
// the last few years (https://inside.java/2021/06/13/podcast-017/).
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    15
// ...Scala already has it for many years and the concept is
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    16
// older than your friendly lecturer, that is stone old  ;o)
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    17
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    18
// The general schema:
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    19
//
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    20
//    expression match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    21
//       case pattern1 => expression1
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    22
//       case pattern2 => expression2
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    23
//       ...
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    24
//       case patternN => expressionN
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    25
//    }
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    26
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    27
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    28
// recall
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    29
def len(xs: List[Int]) : Int = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    30
    if (xs == Nil) 0
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    31
    else 1 + len(xs.tail)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    32
}    
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    33
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    34
def len(xs: List[Int]) : Int = xs match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    35
    case Nil => 0
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    36
    case _::xs => 1 + len(xs)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    37
}  
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    38
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    39
len(Nil)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    40
len(List(1,2,3,4))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    41
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    42
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    43
List(1,2,3,4).map(x => x * x)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    44
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    45
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    46
  lst match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    47
    case Nil => Nil
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    48
    case foo::xs => f(foo) :: my_map_int(xs, f)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    49
  }
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    50
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    51
def my_map_option(opt: Option[Int], f: Int => Int) : Option[Int] = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    52
  opt match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    53
    case None => None
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    54
    case Some(x) => {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    55
      Some(f(x))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    56
    }
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    57
  }
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    58
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    59
my_map_option(None, x => x * x)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    60
my_map_option(Some(8), x => x * x)
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    61
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    62
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    63
// you can also have cases combined
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    64
def season(month: String) : String = month match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    65
  case "March" | "April" | "May" => "It's spring"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    66
  case "June" | "July" | "August" => "It's summer"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    67
  case "September" | "October" | "November" => "It's autumn"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    68
  case "December" => "It's winter"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    69
  case "January" | "February" => "It's unfortunately winter"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    70
  case _ => "Wrong month"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    71
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    72
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    73
// pattern-match on integers
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    74
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    75
def fib(n: Int) : Int = n match { 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    76
  case 0 | 1 => 1
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    77
  case _ => fib(n - 1) + fib(n - 2)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    78
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    79
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    80
fib(10)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    81
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    82
// pattern-match on results
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    83
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    84
// Silly: fizz buzz
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    85
def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    86
  case (0, 0) => "fizz buzz"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    87
  case (0, _) => "fizz"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    88
  case (_, 0) => "buzz"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    89
  case _ => n.toString  
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    90
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    91
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    92
for (n <- 1 to 20) 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    93
 println(fizz_buzz(n))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    94
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    95
// guards in pattern-matching
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    96
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    97
def foo(xs: List[Int]) : String = xs match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    98
  case Nil => s"this list is empty"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
    99
  case x :: xs if x % 2 == 0 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   100
     => s"the first elemnt is even"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   101
  case x if len(x) ==
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   102
     => s"this list has exactly two elements"   
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   103
  case x :: y :: rest if x == y
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   104
     => s"this has two elemnts that are the same"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   105
  case hd :: tl => s"this list is standard $hd::$tl"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   106
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   107
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   108
foo(Nil)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   109
foo(List(1,2,3))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   110
foo(List(1,1))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   111
foo(List(1,1,2,3))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   112
foo(List(2,2,2,3))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   113
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   114
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   115
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   116
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   117
abstract class Colour
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   118
case object Red extends Colour 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   119
case object Green extends Colour 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   120
case object Blue extends Colour
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   121
case object Yellow extends Colour
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   122
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   123
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   124
def fav_colour(c: Colour) : Boolean = c match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   125
  case Green => true
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   126
  case Red => true
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   127
  case _  => false 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   128
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   129
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   130
fav_colour(Blue)
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   131
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   132
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   133
// ... a tiny bit more useful: Roman Numerals
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   134
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   135
sealed abstract class RomanDigit 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   136
case object I extends RomanDigit 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   137
case object V extends RomanDigit 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   138
case object X extends RomanDigit 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   139
case object L extends RomanDigit 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   140
case object C extends RomanDigit 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   141
case object D extends RomanDigit 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   142
case object M extends RomanDigit 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   143
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   144
type RomanNumeral = List[RomanDigit] 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   145
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   146
List(I, M,C,D,X,X,V,I,I, A)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   147
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   148
/*
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   149
I    -> 1
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   150
II   -> 2
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   151
III  -> 3
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   152
IV   -> 4
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   153
V    -> 5
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   154
VI   -> 6
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   155
VII  -> 7
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   156
VIII -> 8
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   157
IX   -> 9
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   158
X    -> 10
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   159
*/
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   160
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   161
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match { 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   162
  case Nil => 0
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   163
  case M::r    => 1000 + RomanNumeral2Int(r)  
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   164
  case C::M::r => 900 + RomanNumeral2Int(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   165
  case D::r    => 500 + RomanNumeral2Int(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   166
  case C::D::r => 400 + RomanNumeral2Int(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   167
  case C::r    => 100 + RomanNumeral2Int(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   168
  case X::C::r => 90 + RomanNumeral2Int(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   169
  case L::r    => 50 + RomanNumeral2Int(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   170
  case X::L::r => 40 + RomanNumeral2Int(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   171
  case X::r    => 10 + RomanNumeral2Int(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   172
  case I::X::r => 9 + RomanNumeral2Int(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   173
  case V::r    => 5 + RomanNumeral2Int(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   174
  case I::V::r => 4 + RomanNumeral2Int(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   175
  case I::r    => 1 + RomanNumeral2Int(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   176
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   177
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   178
RomanNumeral2Int(List(I,V))             // 4
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   179
RomanNumeral2Int(List(I,I,I,I))         // 4 (invalid Roman number)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   180
RomanNumeral2Int(List(V,I))             // 6
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   181
RomanNumeral2Int(List(I,X))             // 9
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   182
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   183
RomanNumeral2Int(List(M,M,X,V,I,I))     // 2017
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   184
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   185
abstract class Tree
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   186
case class Leaf(x: Int)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   187
case class Branch(tl: Tree, tr: Tree)
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   188
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   189
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   190
abstract class Rexp
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   191
case object ZERO extends Rexp                      // matches nothing
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   192
case object ONE extends Rexp                       // matches the empty string
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   193
case class CHAR(c: Char) extends Rexp              // matches a character c
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   194
case class ALT(r1: Rexp, r2: Rexp) extends Rexp    // alternative
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   195
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp    // sequence
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   196
case class STAR(r: Rexp) extends Rexp              // star
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   197
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   198
def depth(r: Rexp) : Int = r match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   199
  case ZERO => 1
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   200
  case ONE => 1
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   201
  case CHAR(_) => 1
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   202
  case ALT(r1, r2) => 1 + List(depth(r1), depth(r2)).max
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   203
  case SEQ(r1, r2) => 1 + List(depth(r1), depth(r2)).max
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   204
  case STAR(r1) => 1 + depth(r1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   205
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   206
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   207
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   208
// Trees (example of an Algebraic Datatype)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   209
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   210
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   211
abstract class Tree
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   212
case class Leaf(x: Int) extends Tree
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   213
case class Node(s: String, left: Tree, right: Tree) extends Tree 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   214
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   215
val lf = Leaf(20)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   216
val tr = Node("foo", Leaf(10), Leaf(23))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   217
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   218
val lst : List[Tree] = List(lf, tr)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   219
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   220
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   221
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   222
// expressions (essentially trees)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   223
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   224
sealed abstract class Exp
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   225
case class N(n: Int) extends Exp                  // for numbers
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   226
case class Plus(e1: Exp, e2: Exp) extends Exp
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   227
case class Times(e1: Exp, e2: Exp) extends Exp
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   228
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   229
def string(e: Exp) : String = e match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   230
  case N(n) => s"$n"
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   231
  case Plus(e1, e2) => s"(${string(e1)} + ${string(e2)})" 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   232
  case Times(e1, e2) => s"(${string(e1)} * ${string(e2)})"
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   233
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   234
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   235
val e = Plus(N(9), Times(N(3), N(4)))
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   236
println(e.toString)
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   237
println(string(e))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   238
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   239
def eval(e: Exp) : Int = e match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   240
  case N(n) => n
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   241
  case Plus(e1, e2) => eval(e1) + eval(e2) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   242
  case Times(e1, e2) => eval(e1) * eval(e2) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   243
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   244
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   245
println(eval(e))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   246
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   247
// simplification rules:
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   248
// e + 0, 0 + e => e 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   249
// e * 0, 0 * e => 0
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   250
// e * 1, 1 * e => e
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   251
//
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   252
// (....9 ....)
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   253
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   254
def simp(e: Exp) : Exp = e match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   255
  case N(n) => N(n)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   256
  case Plus(e1, e2) => (simp(e1), simp(e2)) match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   257
    case (N(0), e2s) => e2s
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   258
    case (e1s, N(0)) => e1s
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   259
    case (e1s, e2s) => Plus(e1s, e2s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   260
  }  
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   261
  case Times(e1, e2) => (simp(e1), simp(e2)) match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   262
    case (N(0), _) => N(0)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   263
    case (_, N(0)) => N(0)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   264
    case (N(1), e2s) => e2s
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   265
    case (e1s, N(1)) => e1s
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   266
    case (e1s, e2s) => Times(e1s, e2s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   267
  }  
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   268
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   269
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   270
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   271
val e2 = Times(Plus(N(0), N(1)), Plus(N(0), N(9)))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   272
println(string(e2))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   273
println(string(simp(e2)))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   274
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   275
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   276
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   277
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   278
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   279
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   280
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   281
// Tokens and Reverse Polish Notation
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   282
abstract class Token
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   283
case class T(n: Int) extends Token
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   284
case object PL extends Token
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   285
case object TI extends Token
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   286
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   287
// transfroming an Exp into a list of tokens
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   288
def rp(e: Exp) : List[Token] = e match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   289
  case N(n) => List(T(n))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   290
  case Plus(e1, e2) => rp(e1) ::: rp(e2) ::: List(PL) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   291
  case Times(e1, e2) => rp(e1) ::: rp(e2) ::: List(TI) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   292
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   293
println(string(e2))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   294
println(rp(e2))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   295
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   296
def comp(ls: List[Token], st: List[Int] = Nil) : Int = (ls, st) match {
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   297
  case (Nil, st) => st.head 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   298
  case (T(n)::rest, st) => comp(rest, n::st)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   299
  case (PL::rest, n1::n2::st) => comp(rest, n1 + n2::st)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   300
  case (TI::rest, n1::n2::st) => comp(rest, n1 * n2::st)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   301
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   302
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   303
comp(rp(e))
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   304
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   305
def proc(s: String) : Token = s match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   306
  case  "+" => PL
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   307
  case  "*" => TI
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   308
  case  _ => T(s.toInt) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   309
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   310
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   311
comp("1 2 + 4 * 5 + 3 +".split(" ").toList.map(proc), Nil)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   312
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   313
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   314
// Tail recursion
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   315
//================
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   316
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   317
def fact(n: BigInt): BigInt = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   318
  if (n == 0) 1 else n * fact(n - 1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   319
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   320
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   321
fact(10)          
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   322
fact(1000)        
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   323
fact(100000)       
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   324
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   325
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   326
def factT(n: BigInt, acc: BigInt): BigInt =
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   327
  if (n == 0) acc else factT(n - 1, n * acc)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   328
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   329
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   330
factT(10, 1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   331
println(factT(100000, 1))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   332
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   333
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   334
// there is a flag for ensuring a function is tail recursive
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   335
import scala.annotation.tailrec
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   336
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   337
@tailrec
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   338
def factT(n: BigInt, acc: BigInt): BigInt =
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   339
  if (n == 0) acc else factT(n - 1, n * acc)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   340
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   341
factT(100000, 1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   342
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   343
// for tail-recursive functions the Scala compiler
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   344
// generates loop-like code, which does not need
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   345
// to allocate stack-space in each recursive
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   346
// call; Scala can do this only for tail-recursive
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   347
// functions
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   348
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   349
// Moral: Whenever a recursive function is resource-critical
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   350
// (i.e. works with a large recursion depth), then you need to
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   351
// write it in tail-recursive fashion.
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   352
// 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   353
// Unfortuantely, Scala because of current limitations in 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   354
// the JVM is not as clever as other functional languages. It can 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   355
// only optimise "self-tail calls". This excludes the cases of 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   356
// multiple functions making tail calls to each other. Well,
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   357
// nothing is perfect. 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   358
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   359
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   360
380
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   361
// Polymorphic Types
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   362
//===================
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   363
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   364
// You do not want to write functions like contains, first, 
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   365
// length and so on for every type of lists.
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   366
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   367
def length_int_list(lst: List[Int]): Int = lst match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   368
  case Nil => 0
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 450
diff changeset
   369
  case _::xs => 1 + length_int_list(xs)
380
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   370
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   371
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   372
length_int_list(List(1, 2, 3, 4))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   373
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   374
def length_string_list(lst: List[String]): Int = lst match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   375
  case Nil => 0
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   376
  case _::xs => 1 + length_string_list(xs)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   377
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   378
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   379
length_string_list(List("1", "2", "3", "4"))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   380
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   381
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   382
// you can make the function parametric in type(s)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   383
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   384
def length[A](lst: List[A]): Int = lst match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   385
  case Nil => 0
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   386
  case x::xs => 1 + length(xs)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   387
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   388
length(List("1", "2", "3", "4"))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   389
length(List(1, 2, 3, 4))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   390
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 450
diff changeset
   391
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 450
diff changeset
   392
length[String](List(1, 2, 3, 4))
380
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   393
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   394
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   395
def map[A, B](lst: List[A], f: A => B): List[B] = lst match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   396
  case Nil => Nil
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   397
  case x::xs => f(x)::map(xs, f) 
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   398
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   399
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   400
map(List(1, 2, 3, 4), (x: Int) => x.toString)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   401
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   402
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   403
// should be
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   404
def first[A, B](xs: List[A], f: A => Option[B]) : Option[B] = ???
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   405
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   406
// Type inference is local in Scala
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   407
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   408
def id[T](x: T) : T = x
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   409
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   410
val x = id(322)          // Int
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   411
val y = id("hey")        // String
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   412
val z = id(Set(1,2,3,4)) // Set[Int]
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   413
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   414
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   415
// The type variable concept in Scala can get really complicated.
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   416
//
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   417
// - variance (OO)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   418
// - bounds (subtyping)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   419
// - quantification
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   420
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   421
// Java has issues with this too: Java allows
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   422
// to write the following incorrect code, and
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   423
// only recovers by raising an exception
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   424
// at runtime.
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   425
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   426
// Object[] arr = new Integer[10];
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   427
// arr[0] = "Hello World";
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   428
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   429
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   430
// Scala gives you a compile-time error, which
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   431
// is much better.
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   432
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   433
var arr = Array[Int]()
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   434
arr(0) = "Hello World"
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   435
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   436
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   437
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   438
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   439
// Function definitions again
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   440
//============================
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   441
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   442
// variable arguments
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   443
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   444
def printAll(strings: String*) = {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   445
  strings.foreach(println)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   446
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   447
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   448
printAll()
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   449
printAll("foo")
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   450
printAll("foo", "bar")
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   451
printAll("foo", "bar", "baz")
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   452
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   453
// pass a list to the varargs field
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   454
val fruits = List("apple", "banana", "cherry")
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   455
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   456
printAll(fruits: _*)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   457
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   458
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   459
// you can also implement your own string interpolations
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   460
import scala.language.implicitConversions
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   461
import scala.language.reflectiveCalls
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   462
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   463
implicit def sring_inters(sc: StringContext) = new {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   464
    def i(args: Any*): String = s"${sc.s(args:_*)}\n"
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   465
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   466
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   467
i"add ${3+2} ${3 * 3}" 
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   468
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   469
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   470
// default arguments
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   471
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   472
def length[A](xs: List[A]) : Int = xs match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   473
  case Nil => 0
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   474
  case _ :: tail => 1 + length(tail)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   475
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   476
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   477
def lengthT[A](xs: List[A], acc : Int = 0) : Int = xs match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   478
  case Nil => acc
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   479
  case _ :: tail => lengthT(tail, 1 + acc)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   480
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   481
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   482
lengthT(List.fill(100000)(1))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   483
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   484
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   485
def fact(n: BigInt, acc: BigInt = 1): BigInt =
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   486
  if (n == 0) acc else fact(n - 1, n * acc)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   487
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   488
fact(10)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   489
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   490
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   491
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   492
// currying    (Haskell Curry)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   493
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   494
def add(x: Int, y: Int) = x + y
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   495
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   496
List(1,2,3,4,5).map(x => add(3, x))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   497
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   498
def add2(x: Int)(y: Int) = x + y
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   499
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   500
List(1,2,3,4,5).map(add2(3))
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   501
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   502
val a3 : Int => Int = add2(3)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   503
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   504
// currying helps sometimes with type inference
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   505
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   506
def find[A](xs: List[A])(pred: A => Boolean): Option[A] = {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   507
  xs match {
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   508
    case Nil => None
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   509
    case hd :: tl =>
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   510
      if (pred(hd)) Some(hd) else find(tl)(pred)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   511
  }
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   512
}
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   513
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   514
find(List(1, 2, 3))(x => x % 2 == 0)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   515
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   516
// Source.fromURL(url)(encoding)
d19b0a50ceb9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 326
diff changeset
   517
// Source.fromFile(name)(encoding)
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   518
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   519
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   520
382
1bd800376e0c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 381
diff changeset
   521
1bd800376e0c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 381
diff changeset
   522
1bd800376e0c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 381
diff changeset
   523
1bd800376e0c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 381
diff changeset
   524
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   525
// Sudoku 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   526
//========
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   527
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   528
// THE POINT OF THIS CODE IS NOT TO BE SUPER
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   529
// EFFICIENT AND FAST, just explaining exhaustive
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   530
// depth-first search
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   531
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   532
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   533
val game0 = """.14.6.3..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   534
              |62...4..9
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   535
              |.8..5.6..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   536
              |.6.2....3
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   537
              |.7..1..5.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   538
              |5....9.6.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   539
              |..6.2..3.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   540
              |1..5...92
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   541
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   542
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   543
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   544
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   545
type Pos = (Int, Int)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   546
val EmptyValue = '.'
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   547
val MaxValue = 9
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   548
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   549
def pretty(game: String): String = 
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   550
  "\n" + (game.grouped(MaxValue).mkString("\n"))
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   551
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   552
pretty(game0)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   553
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   554
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   555
val allValues = "123456789".toList
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   556
val indexes = (0 to 8).toList
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   557
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   558
def empty(game: String) = game.indexOf(EmptyValue)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   559
def isDone(game: String) = empty(game) == -1 
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   560
def emptyPosition(game: String) = {
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   561
  val e = empty(game)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   562
  (e % MaxValue, e / MaxValue)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   563
}
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   564
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   565
def get_row(game: String, y: Int) = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   566
  indexes.map(col => game(y * MaxValue + col))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   567
def get_col(game: String, x: Int) = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   568
  indexes.map(row => game(x + row * MaxValue))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   569
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   570
//get_row(game0, 0)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   571
//get_row(game0, 1)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   572
//get_col(game0, 0)
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   573
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   574
def get_box(game: String, pos: Pos): List[Char] = {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   575
    def base(p: Int): Int = (p / 3) * 3
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   576
    val x0 = base(pos._1)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   577
    val y0 = base(pos._2)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   578
    val ys = (y0 until y0 + 3).toList
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   579
    (x0 until x0 + 3).toList
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   580
      .flatMap(x => ys.map(y => game(x + y * MaxValue)))
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   581
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   582
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   583
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   584
//get_box(game0, (3, 1))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   585
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   586
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   587
// this is not mutable!!
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   588
def update(game: String, pos: Int, value: Char): String = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   589
  game.updated(pos, value)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   590
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   591
def toAvoid(game: String, pos: Pos): List[Char] = 
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   592
  (get_col(game, pos._1) ++ 
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   593
   get_row(game, pos._2) ++ 
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   594
   get_box(game, pos))
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   595
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   596
def candidates(game: String, pos: Pos): List[Char] = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   597
  allValues.diff(toAvoid(game, pos))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   598
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   599
//candidates(game0, (0,0))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   600
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   601
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   602
def search(game: String): List[String] = {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   603
  if (isDone(game)) List(game)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   604
  else {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   605
    val cs = candidates(game, emptyPosition(game))
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   606
    cs.map(c => search(update(game, empty(game), c))).flatten
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   607
  }
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   608
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   609
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   610
pretty(game0)
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   611
search(game0).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   612
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   613
val game1 = """23.915...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   614
              |...2..54.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   615
              |6.7......
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   616
              |..1.....9
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   617
              |89.5.3.17
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   618
              |5.....6..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   619
              |......9.5
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   620
              |.16..7...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   621
              |...329..1""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   622
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   623
search(game1).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   624
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   625
// a game that is in the hard category
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   626
val game2 = """8........
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   627
              |..36.....
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   628
              |.7..9.2..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   629
              |.5...7...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   630
              |....457..
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   631
              |...1...3.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   632
              |..1....68
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   633
              |..85...1.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   634
              |.9....4..""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   635
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   636
search(game2).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   637
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   638
// game with multiple solutions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   639
val game3 = """.8...9743
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   640
              |.5...8.1.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   641
              |.1.......
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   642
              |8....5...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   643
              |...8.4...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   644
              |...3....6
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   645
              |.......7.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   646
              |.3.5...8.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   647
              |9724...5.""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   648
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   649
search(game3).map(pretty).foreach(println)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   650
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   651
// for measuring time
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   652
def time_needed[T](i: Int, code: => T) = {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   653
  val start = System.nanoTime()
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   654
  for (j <- 1 to i) code
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   655
  val end = System.nanoTime()
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   656
  s"${(end - start) / 1.0e9} secs"
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   657
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   658
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   659
time_needed(1, search(game2))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   660
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   661
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   662
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   663
// tail recursive version that searches 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   664
// for all Sudoku solutions
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   665
import scala.annotation.tailrec
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   666
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   667
@tailrec
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   668
def searchT(games: List[String], sols: List[String]): List[String] = 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   669
 games match {
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   670
    case Nil => sols
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   671
    case game::rest => {
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   672
      if (isDone(game)) searchT(rest, game::sols)
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   673
      else {
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   674
        val cs = candidates(game, emptyPosition(game))
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   675
        searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols)
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   676
     }
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   677
   }
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   678
 }
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   679
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   680
searchT(List(game3), List()).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   681
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   682
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   683
// tail recursive version that searches 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   684
// for a single solution
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   685
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   686
def search1T(games: List[String]): Option[String] = games match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   687
  case Nil => None
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   688
  case game::rest => {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   689
    if (isDone(game)) Some(game)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   690
    else {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   691
      val cs = candidates(game, emptyPosition(game))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   692
      search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   693
    }
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   694
  }
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   695
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   696
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   697
search1T(List(game3)).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   698
time_needed(1, search1T(List(game3)))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   699
time_needed(1, search1T(List(game2)))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   700
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   701
// game with multiple solutions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   702
val game3 = """.8...9743
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   703
              |.5...8.1.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   704
              |.1.......
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   705
              |8....5...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   706
              |...8.4...
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   707
              |...3....6
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   708
              |.......7.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   709
              |.3.5...8.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   710
              |9724...5.""".stripMargin.replaceAll("\\n", "")
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   711
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   712
searchT(List(game3), Nil).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   713
search1T(List(game3)).map(pretty)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   714
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   715
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   716
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   717
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   718
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   719
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   720
// Cool Stuff in Scala
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   721
//=====================
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   722
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   723
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   724
// Implicits or How to Pimp your Library
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   725
//======================================
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   726
//
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   727
// For example adding your own methods to Strings:
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   728
// Imagine you want to increment strings, like
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   729
//
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   730
//     "HAL".increment
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   731
//
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   732
// you can avoid ugly fudges, like a MyString, by
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   733
// using implicit conversions.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   734
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 450
diff changeset
   735
print("\n")
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 450
diff changeset
   736
print("""\n""")
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   737
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   738
implicit class MyString(s: String) {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   739
  def increment = s.map(c => (c + 1).toChar) 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   740
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   741
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 450
diff changeset
   742
"HAL".increment
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   743
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   744
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   745
// Abstract idea:
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   746
// In that version implicit conversions were used to solve the 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   747
// late extension problem; namely, given a class C and a class T, 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   748
// how to have C extend T without touching or recompiling C. 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   749
// Conversions add a wrapper when a member of T is requested 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   750
// from an instance of C.
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   751
381
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   752
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   753
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   754
import scala.concurrent.duration.{TimeUnit,SECONDS,MINUTES}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   755
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   756
case class Duration(time: Long, unit: TimeUnit) {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   757
  def +(o: Duration) = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   758
    Duration(time + unit.convert(o.time, o.unit), unit)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   759
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   760
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   761
implicit class Int2Duration(that: Int) {
381
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   762
  def seconds = Duration(that, SECONDS)
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   763
  def minutes = Duration(that, MINUTES)
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   764
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   765
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   766
5.seconds + 2.minutes   //Duration(125L, SECONDS )
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   767
2.minutes + 60.seconds
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   768
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   769
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   770
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   771
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   772
// Regular expressions - the power of DSLs in Scala
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   773
//==================================================
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   774
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   775
abstract class Rexp
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   776
case object ZERO extends Rexp                     // nothing
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   777
case object ONE extends Rexp                      // the empty string
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   778
case class CHAR(c: Char) extends Rexp             // a character c
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   779
case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative  r1 + r2
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   780
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence     r1 . r2  
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   781
case class STAR(r: Rexp) extends Rexp             // star         r*
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   782
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   783
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   784
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   785
// writing (ab)* in the format above is 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   786
// tedious
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   787
val r0 = STAR(SEQ(CHAR('a'), CHAR('b')))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   788
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   789
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   790
// some convenience for typing in regular expressions
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   791
import scala.language.implicitConversions    
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   792
import scala.language.reflectiveCalls 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   793
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   794
def charlist2rexp(s: List[Char]): Rexp = s match {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   795
  case Nil => ONE
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   796
  case c::Nil => CHAR(c)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   797
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   798
}
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   799
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   800
implicit def string2rexp(s: String): Rexp = 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   801
  charlist2rexp(s.toList)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   802
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   803
val r1 = STAR("ab")
381
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   804
val r2 = STAR("hello") | STAR("world")
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   805
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   806
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   807
implicit def RexpOps (r: Rexp) = new {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   808
  def | (s: Rexp) = ALT(r, s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   809
  def % = STAR(r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   810
  def ~ (s: Rexp) = SEQ(r, s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   811
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   812
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   813
implicit def stringOps (s: String) = new {
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   814
  def | (r: Rexp) = ALT(s, r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   815
  def | (r: String) = ALT(s, r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   816
  def % = STAR(s)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   817
  def ~ (r: Rexp) = SEQ(s, r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   818
  def ~ (r: String) = SEQ(s, r)
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   819
}
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   820
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   821
//example regular expressions
381
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   822
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   823
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   824
val digit = ("0" | "1" | "2" | "3" | "4" | 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
   825
              "5" | "6" | "7" | "8" | "9")
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   826
val sign = "+" | "-" | ""
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   827
val number = sign ~ digit ~ digit.% 
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   828
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   829
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   830
381
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   831
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   832
// In mandelbrot.scala I used complex (imaginary) numbers 
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   833
// and implemented the usual arithmetic operations for complex 
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   834
// numbers.
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   835
381
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   836
case class Complex(re: Double, im: Double) { 
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   837
  // represents the complex number re + im * i
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   838
  def +(that: Complex) = Complex(this.re + that.re, this.im + that.im)
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   839
  def -(that: Complex) = Complex(this.re - that.re, this.im - that.im)
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   840
  def *(that: Complex) = Complex(this.re * that.re - this.im * that.im,
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   841
                                 this.re * that.im + that.re * this.im)
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   842
  def *(that: Double) = Complex(this.re * that, this.im * that)
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   843
  def abs = Math.sqrt(this.re * this.re + this.im * this.im)
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   844
}
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   845
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   846
val test = Complex(1, 2) + Complex (3, 4)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   847
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   848
381
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   849
// ...to allow the notation n + m * i
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   850
import scala.language.implicitConversions   
325
ca9c1cf929fa updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
   851
381
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   852
val i = Complex(0, 1)
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   853
implicit def double2complex(re: Double) = Complex(re, 0)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   854
381
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   855
val inum1 = -2.0 + -1.5 * i
116fa3c8584f updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   856
val inum2 =  1.0 +  1.5 * i