progs/lecture5.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 08 Dec 2023 00:54:36 +0000
changeset 481 e03a0100ec46
parent 470 86a456f8cb92
child 482 769bda18a43d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
238
046f37a262d0 updated
updated
parents: 226
diff changeset
     1
// Scala Lecture 5
222
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: 470
diff changeset
     4
// extension methods
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
     5
// implicit conversions
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
     6
// (Immutable) OOP
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
     7
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
     8
// Cool Stuff in Scala
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
     9
//=====================
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    10
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    11
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    12
// Extensions or How to Pimp your Library
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    13
//======================================
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    14
//
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    15
// For example adding your own methods to Strings:
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    16
// Imagine you want to increment strings, like
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    17
//
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    18
//     "HAL".increment
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    19
//
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    20
// you can avoid ugly fudges, like a MyString, by
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    21
// using implicit conversions.
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    22
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    23
extension (s: String) {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    24
  def increment = s.map(c => (c + 1).toChar)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    25
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    26
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    27
"HAL".increment
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    28
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    29
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    30
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    31
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    32
import scala.concurrent.duration.{TimeUnit,SECONDS,MINUTES}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    33
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    34
case class Duration(time: Long, unit: TimeUnit) {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    35
  def +(o: Duration) = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    36
    Duration(time + unit.convert(o.time, o.unit), unit)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    37
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    38
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    39
extension (that: Int) {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    40
  def seconds = Duration(that, SECONDS)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    41
  def minutes = Duration(that, MINUTES)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    42
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    43
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    44
2.minutes + 60.seconds
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    45
5.seconds + 2.minutes   //Duration(125, SECONDS )
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    46
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    47
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    48
// Regular expressions - the power of DSLs in Scala
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    49
//                                     and Laziness
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    50
//==================================================
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    51
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    52
abstract class Rexp
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    53
case object ZERO extends Rexp                     // nothing
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    54
case object ONE extends Rexp                      // the empty string
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    55
case class CHAR(c: Char) extends Rexp             // a character c
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    56
case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative  r1 + r2
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    57
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence     r1 . r2  
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    58
case class STAR(r: Rexp) extends Rexp             // star         r*
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    59
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    60
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    61
// some convenience for typing in regular expressions
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    62
import scala.language.implicitConversions    
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    63
import scala.language.reflectiveCalls 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    64
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    65
def charlist2rexp(s: List[Char]): Rexp = s match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    66
  case Nil => ONE
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    67
  case c::Nil => CHAR(c)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    68
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    69
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    70
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    71
given Conversion[String, Rexp] = (s => charlist2rexp(s.toList))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    72
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    73
extension (r: Rexp) {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    74
  def | (s: Rexp) = ALT(r, s)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    75
  def % = STAR(r)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    76
  def ~ (s: Rexp) = SEQ(r, s)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    77
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    78
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    79
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    80
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    81
//example regular expressions
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    82
val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    83
val sign = "+" | "-" | ""
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    84
val number = sign ~ digit ~ digit.% 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    85
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    86
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    87
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    88
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    89
// Object Oriented Programming in Scala
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    90
// =====================================
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    91
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
    92
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
    93
abstract class Animal 
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    94
case class Bird(name: String) extends Animal {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    95
   override def toString = name
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    96
}
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    97
case class Mammal(name: String) extends Animal
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    98
case class Reptile(name: String) extends Animal
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    99
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   100
Mammal("Zebra")
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   101
println(Mammal("Zebra"))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   102
println(Mammal("Zebra").toString)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   103
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   104
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   105
Bird("Sparrow")
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   106
println(Bird("Sparrow"))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   107
println(Bird("Sparrow").toString)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   108
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   109
Bird("Sparrow").copy(name = "House Sparrow")
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   110
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   111
def group(a : Animal) = a match {
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   112
  case Bird(_) => "It's a bird"
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   113
  case Mammal(_) => "It's a mammal"
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   114
}
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   115
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   116
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   117
// There is a very convenient short-hand notation
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   118
// for constructors:
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   119
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   120
class Fraction(x: Int, y: Int) {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   121
  def numer = x
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   122
  def denom = y
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   123
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   124
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   125
val half = Fraction(1, 2)
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   126
half.numer
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   127
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   128
// does not work with "vanilla" classes
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   129
half match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   130
  case Fraction(x, y) => x / y
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   131
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   132
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   133
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   134
case class Fraction(numer: Int, denom: Int)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   135
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   136
val half = Fraction(1, 2)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   137
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   138
half.numer
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   139
half.denom
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   140
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   141
// works with case classes
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   142
half match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   143
  case Fraction(x, y) => x / y
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   144
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   145
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   146
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   147
// In mandelbrot.scala I used complex (imaginary) numbers 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   148
// and implemented the usual arithmetic operations for complex 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   149
// numbers.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   150
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   151
case class Complex(re: Double, im: Double) { 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   152
  // represents the complex number re + im * i
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   153
  def +(that: Complex) = Complex(this.re + that.re, this.im + that.im)
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   154
  def -(that: Complex) = Complex(this.re - that.re, this.im - that.im)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   155
  def *(that: Complex) = Complex(this.re * that.re - this.im * that.im,
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   156
                                 this.re * that.im + that.re * this.im)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   157
  def *(that: Double) = Complex(this.re * that, this.im * that)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   158
  def abs = Math.sqrt(this.re * this.re + this.im * this.im)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   159
}
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   160
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   161
// usual way to reference methods
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   162
//object.method(....)
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   163
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   164
val test = Complex(1, 2) + (Complex (3, 4))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   165
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   166
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   167
import scala.language.postfixOps
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   168
(List(5,4,3,2,1) sorted) reverse
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   169
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   170
// this could have equally been written as
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   171
val test = Complex(1, 2).+(Complex (3, 4))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   172
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   173
// this applies to all methods, but requires
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   174
import scala.language.postfixOps
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   175
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   176
List(5, 2, 3, 4).sorted
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   177
List(5, 2, 3, 4) sorted
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   178
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   179
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   180
// ...to allow the notation n + m * i
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   181
import scala.language.implicitConversions   
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   182
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   183
val i = Complex(0, 1)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   184
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   185
given Conversion[Double, Complex] = (re => Complex(re, 0))
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   186
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   187
val inum1 = -2.0 + -1.5 * i
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   188
val inum2 =  1.0 +  1.5 * i
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   189
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   190
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   191
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   192
// All is public by default....so no public is needed.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   193
// You can have the usual restrictions about private 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   194
// values and methods, if you are MUTABLE !!!
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   195
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   196
case class BankAccount(init: Int) {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   197
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   198
  private var balance = init
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   199
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   200
  def deposit(amount: Int): Unit = {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   201
    if (amount > 0) balance = balance + amount
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   202
  }
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   203
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   204
  def withdraw(amount: Int): Int =
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   205
    if (0 < amount && amount <= balance) {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   206
      balance = balance - amount
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   207
      balance
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   208
    } else throw new Error("insufficient funds")
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   209
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   210
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   211
// BUT since we are completely IMMUTABLE, this is 
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   212
// virtually of no concern to us.
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   213
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   214
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   215
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   216
// another example about Fractions
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   217
import scala.language.implicitConversions
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   218
import scala.language.reflectiveCalls
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   219
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   220
case class Fraction(numer: Int, denom: Int) {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   221
  override def toString = numer.toString + "/" + denom.toString
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   222
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   223
  def +(other: Fraction) = 
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   224
    Fraction(numer * other.denom + other.numer * denom, 
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   225
             denom * other.denom)
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   226
  def *(other: Fraction) = 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   227
    Fraction(numer * other.numer, denom * other.denom)
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   228
 }
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   229
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   230
given Conversion[Int, Fraction] = (x => Fraction(x, 1))
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   231
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   232
val half = Fraction(1, 2)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   233
val third = Fraction (1, 3)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   234
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   235
half + third
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   236
half * third
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   237
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   238
1 + half
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   239
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   240
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   241
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   242
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   243
// DFAs in Scala  
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   244
//===============
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   245
import scala.util.Try
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   246
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   247
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   248
// A is the state type
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   249
// C is the input (usually characters)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   250
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   251
case class DFA[A, C](start: A,              // starting state
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   252
                     delta: (A, C) => A,    // transition function
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   253
                     fins:  A => Boolean) { // final states (Set)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   254
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   255
  def deltas(q: A, s: List[C]) : A = s match {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   256
    case Nil => q
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   257
    case c::cs => deltas(delta(q, c), cs)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   258
  }
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   259
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   260
  def accepts(s: List[C]) : Boolean = 
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   261
    Try(fins(deltas(start, s))).getOrElse(false)
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   262
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   263
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   264
// the example shown in the handout 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   265
abstract class State
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   266
case object Q0 extends State
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   267
case object Q1 extends State
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   268
case object Q2 extends State
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   269
case object Q3 extends State
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   270
case object Q4 extends State
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   271
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   272
val delta : (State, Char) => State = 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   273
  { case (Q0, 'a') => Q1
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   274
    case (Q0, 'b') => Q2
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   275
    case (Q1, 'a') => Q4
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   276
    case (Q1, 'b') => Q2
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   277
    case (Q2, 'a') => Q3
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   278
    case (Q2, 'b') => Q2
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   279
    case (Q3, 'a') => Q4
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   280
    case (Q3, 'b') => Q0
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   281
    case (Q4, 'a') => Q4
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   282
    case (Q4, 'b') => Q4 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   283
    case _ => throw new Exception("Undefined") }
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   284
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   285
val dfa = DFA(Q0, delta, Set[State](Q4))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   286
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   287
dfa.accepts("abaaa".toList)     // true
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   288
dfa.accepts("bbabaab".toList)   // true
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   289
dfa.accepts("baba".toList)      // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   290
dfa.accepts("abc".toList)       // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   291
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   292
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   293
// NFAs (Nondeterministic Finite Automata)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   294
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   295
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   296
case class NFA[A, C](starts: Set[A],          // starting states
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   297
                     delta: (A, C) => Set[A], // transition function
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   298
                     fins:  A => Boolean) {   // final states 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   299
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   300
  // given a state and a character, what is the set of 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   301
  // next states? if there is none => empty set
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   302
  def next(q: A, c: C) : Set[A] = 
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   303
    Try(delta(q, c)).getOrElse(Set[A]()) 
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   304
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   305
  def nexts(qs: Set[A], c: C) : Set[A] =
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   306
    qs.flatMap(next(_, c))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   307
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   308
  // depth-first version of accepts
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   309
  def search(q: A, s: List[C]) : Boolean = s match {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   310
    case Nil => fins(q)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   311
    case c::cs => next(q, c).exists(search(_, cs))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   312
  }
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   313
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   314
  def accepts(s: List[C]) : Boolean =
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   315
    starts.exists(search(_, s))
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   316
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   317
046f37a262d0 updated
updated
parents: 226
diff changeset
   318
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   319
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   320
// NFA examples
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   321
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   322
val nfa_trans1 : (State, Char) => Set[State] = 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   323
  { case (Q0, 'a') => Set(Q0, Q1) 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   324
    case (Q0, 'b') => Set(Q2) 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   325
    case (Q1, 'a') => Set(Q1) 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   326
    case (Q2, 'b') => Set(Q2) }
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   327
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   328
val nfa = NFA(Set[State](Q0), nfa_trans1, Set[State](Q2))
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   329
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   330
nfa.accepts("aa".toList)             // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   331
nfa.accepts("aaaaa".toList)          // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   332
nfa.accepts("aaaaab".toList)         // true
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   333
nfa.accepts("aaaaabbb".toList)       // true
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   334
nfa.accepts("aaaaabbbaaa".toList)    // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   335
nfa.accepts("ac".toList)             // false
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   336
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   337
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   338
// Q: Why the kerfuffle about the polymorphic types in DFAs/NFAs?
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   339
// A: Subset construction. Here the state type for the DFA is
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   340
//    sets of states.
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   341
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 380
diff changeset
   342
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   343
def subset[A, C](nfa: NFA[A, C]) : DFA[Set[A], C] = {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   344
  DFA(nfa.starts, 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   345
      { case (qs, c) => nfa.nexts(qs, c) }, 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   346
      _.exists(nfa.fins))
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   347
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   348
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   349
subset(nfa).accepts("aa".toList)             // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   350
subset(nfa).accepts("aaaaa".toList)          // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   351
subset(nfa).accepts("aaaaab".toList)         // true
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   352
subset(nfa).accepts("aaaaabbb".toList)       // true
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   353
subset(nfa).accepts("aaaaabbbaaa".toList)    // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   354
subset(nfa).accepts("ac".toList)             // false
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   355
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   356
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   357
// Laziness with style
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   358
//=====================
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   359
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   360
// The concept of lazy evaluation doesn’t really 
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   361
// exist in non-functional languages. C-like languages
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   362
// are (sort of) strict. To see the difference, consider
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   363
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   364
def square(x: Int) = x * x
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   365
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   366
square(42 + 8)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   367
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   368
// This is called "strict evaluation".
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   369
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   370
// In contrast say we have a pretty expensive operation:
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   371
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   372
def peop(n: BigInt): Boolean = peop(n + 1) 
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   373
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   374
val a = "foo"
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   375
val b = "foo"
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   376
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   377
if (a == b || peop(0)) println("true") else println("false")
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   378
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   379
// This is called "lazy evaluation":
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   380
// you delay compuation until it is really 
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   381
// needed. Once calculated though, the result
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   382
// does not need to be re-calculated.
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   383
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   384
// A useful example is
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   385
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   386
def time_needed[T](i: Int, code: => T) = {
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   387
  val start = System.nanoTime()
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   388
  for (j <- 1 to i) code
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   389
  val end = System.nanoTime()
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   390
  f"${(end - start) / (i * 1.0e9)}%.6f secs"
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   391
}
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   392
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   393
// A slightly less obvious example: Prime Numbers.
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   394
// (I do not care how many) primes: 2, 3, 5, 7, 9, 11, 13 ....
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   395
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   396
def generatePrimes (s: LazyList[Int]): LazyList[Int] =
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   397
  s.head #:: generatePrimes(s.tail.filter(_ % s.head != 0))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   398
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   399
val primes = generatePrimes(LazyList.from(2))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   400
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   401
// the first 10 primes
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   402
primes.take(100).toList
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   403
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   404
time_needed(1, primes.filter(_ > 100).take(3000).toList)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   405
time_needed(1, primes.filter(_ > 100).take(3000).toList)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   406
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   407
// A Stream (LazyList) of successive numbers:
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   408
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   409
LazyList.from(2).take(10)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   410
LazyList.from(2).take(10).force
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   411
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   412
// An Iterative version of the Fibonacci numbers
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   413
def fibIter(a: BigInt, b: BigInt): LazyList[BigInt] =
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   414
  a #:: fibIter(b, a + b)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   415
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   416
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   417
fibIter(1, 1).take(10).force
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   418
fibIter(8, 13).take(10).force
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   419
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   420
fibIter(1, 1).drop(10000).take(1)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   421
fibIter(1, 1).drop(10000).take(1).force
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   422
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   423
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   424
// LazyLists are good for testing
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   425
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   426
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   427
// Regular expressions - the power of DSLs in Scala
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   428
//                                     and Laziness
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   429
//==================================================
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   430
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   431
abstract class Rexp
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   432
case object ZERO extends Rexp                     // nothing
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   433
case object ONE extends Rexp                      // the empty string
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   434
case class CHAR(c: Char) extends Rexp             // a character c
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   435
case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative  r1 + r2
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   436
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence     r1 . r2  
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   437
case class STAR(r: Rexp) extends Rexp             // star         r*
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   438
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   439
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   440
// some convenience for typing in regular expressions
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   441
import scala.language.implicitConversions    
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   442
import scala.language.reflectiveCalls 
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   443
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   444
def charlist2rexp(s: List[Char]): Rexp = s match {
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   445
  case Nil => ONE
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   446
  case c::Nil => CHAR(c)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   447
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   448
}
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   449
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   450
given Conversion[String, Rexp] = (s => charlist2rexp(s.toList))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   451
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   452
extension (r: Rexp) {
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   453
  def | (s: Rexp) = ALT(r, s)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   454
  def % = STAR(r)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   455
  def ~ (s: Rexp) = SEQ(r, s)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   456
}
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   457
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   458
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   459
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   460
//example regular expressions
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   461
val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   462
val sign = "+" | "-" | ""
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   463
val number = sign ~ digit ~ digit.% 
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   464
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   465
// Task: enumerate exhaustively regular expressions
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   466
// starting from small ones towards bigger ones.
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   467
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   468
// 1st idea: enumerate them all in a Set
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   469
// up to a level
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   470
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   471
def enuml(l: Int, s: String) : Set[Rexp] = l match {
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   472
  case 0 => Set(ZERO, ONE) ++ s.map(CHAR).toSet
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   473
  case n =>  
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   474
    val rs = enuml(n - 1, s)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   475
    rs ++
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   476
    (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) ++
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   477
    (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) ++
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   478
    (for (r1 <- rs) yield STAR(r1))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   479
}
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   480
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   481
enuml(1, "a")
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   482
enuml(1, "a").size
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   483
enuml(2, "a").size
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   484
enuml(3, "a").size 
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   485
enuml(4, "a").size // out of heap space
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   486
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   487
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   488
def enum(rs: LazyList[Rexp]) : LazyList[Rexp] = 
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   489
  rs #::: enum( (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #:::
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   490
                (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #:::
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   491
                (for (r1 <- rs) yield STAR(r1)) )
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   492
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   493
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   494
enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(200).force
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 455
diff changeset
   495
enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(5_000_000).force // out of memory
455
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   496
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   497
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   498
def depth(r: Rexp) : Int = r match {
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   499
  case ZERO => 0
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   500
  case ONE => 0
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   501
  case CHAR(_) => 0
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   502
  case ALT(r1, r2) => Math.max(depth(r1), depth(r2)) + 1
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   503
  case SEQ(r1, r2) => Math.max(depth(r1), depth(r2)) + 1 
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   504
  case STAR(r1) => depth(r1) + 1
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   505
}
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   506
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   507
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   508
val is = 
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   509
  (enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b')))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   510
    .dropWhile(depth(_) < 3)
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   511
    .take(10).foreach(println))
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   512
557d18cce0f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 418
diff changeset
   513
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   514
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   515
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   516
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   517
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   518
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   519
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   520
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   521
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 383
diff changeset
   522
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   523
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   524
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   525
// The End ... Almost Christmas
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   526
//===============================
046f37a262d0 updated
updated
parents: 226
diff changeset
   527
046f37a262d0 updated
updated
parents: 226
diff changeset
   528
// I hope you had fun!
046f37a262d0 updated
updated
parents: 226
diff changeset
   529
046f37a262d0 updated
updated
parents: 226
diff changeset
   530
// A function should do one thing, and only one thing.
046f37a262d0 updated
updated
parents: 226
diff changeset
   531
046f37a262d0 updated
updated
parents: 226
diff changeset
   532
// Make your variables immutable, unless there's a good 
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   533
// reason not to. Usually there is not.
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   534
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   535
// I did it once, but this is actually not a good reason:
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   536
// generating new labels:
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   537
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   538
var counter = -1
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   539
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   540
def Fresh(x: String) = {
046f37a262d0 updated
updated
parents: 226
diff changeset
   541
  counter += 1
046f37a262d0 updated
updated
parents: 226
diff changeset
   542
  x ++ "_" ++ counter.toString()
046f37a262d0 updated
updated
parents: 226
diff changeset
   543
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   544
046f37a262d0 updated
updated
parents: 226
diff changeset
   545
Fresh("x")
046f37a262d0 updated
updated
parents: 226
diff changeset
   546
Fresh("x")
046f37a262d0 updated
updated
parents: 226
diff changeset
   547
046f37a262d0 updated
updated
parents: 226
diff changeset
   548
046f37a262d0 updated
updated
parents: 226
diff changeset
   549
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   550
// I think you can be productive on Day 1, but the 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   551
// language is deep.
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   552
//
046f37a262d0 updated
updated
parents: 226
diff changeset
   553
// http://scalapuzzlers.com
046f37a262d0 updated
updated
parents: 226
diff changeset
   554
//
046f37a262d0 updated
updated
parents: 226
diff changeset
   555
// http://www.latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/
046f37a262d0 updated
updated
parents: 226
diff changeset
   556
328
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   557
val two   = 0.2
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   558
val one   = 0.1
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   559
val eight = 0.8
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   560
val six   = 0.6
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   561
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   562
two - one == one
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   563
eight - six == two
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   564
eight - six
328
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   565
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   566
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   567
// problems about equality and type-errors
328
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   568
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   569
List(1, 2, 3).contains("your cup")   // should not compile, but retruns false
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   570
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   571
List(1, 2, 3) == Vector(1, 2, 3)     // again should not compile, but returns true
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   572
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   573
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   574