progs/lecture4.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 01 Aug 2019 09:48:34 +0100
changeset 268 e43f7e92ba26
parent 243 9bb36426c781
child 278 0c2481cd8b1c
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     5
// Polymorphic Types
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     6
//===================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     7
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     8
// You do not want to write functions like contains, first, 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     9
// length and so on for every type of lists.
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    10
224
42d760984496 updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    11
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    12
def length_string_list(lst: List[String]): Int = lst match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    13
  case Nil => 0
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    14
  case x::xs => 1 + length_string_list(xs)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    15
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    16
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    17
def length_int_list(lst: List[Int]): Int = lst match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    18
  case Nil => 0
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    19
  case x::xs => 1 + length_int_list(xs)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    20
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    21
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    22
length_string_list(List("1", "2", "3", "4"))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    23
length_int_list(List(1, 2, 3, 4))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    24
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    25
//-----
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    26
def length[A](lst: List[A]): Int = lst match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    27
  case Nil => 0
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    28
  case x::xs => 1 + length(xs)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    29
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    30
length(List("1", "2", "3", "4"))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    31
length(List(1, 2, 3, 4))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    32
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    33
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    34
def map[A, B](lst: List[A], f: A => B): List[B] = lst match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    35
  case Nil => Nil
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    36
  case x::xs => f(x)::map(xs, f) 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    37
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    38
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    39
map(List(1, 2, 3, 4), (x: Int) => x.toString)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    40
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    41
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    42
// Remember?
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    43
def first[A, B](xs: List[A], f: A => Option[B]) : Option[B] = ...
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    44
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    45
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    46
// distinct / distinctBy
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    47
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    48
val ls = List(1,2,3,3,2,4,3,2,1)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    49
ls.distinct
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    50
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    51
ls.minBy(_._2)
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    52
ls.sortBy(_._1)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    53
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    54
def distinctBy[B, C](xs: List[B], 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    55
                     f: B => C, 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    56
                     acc: List[C] = Nil): List[B] = xs match {
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  case Nil => Nil
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    58
  case x::xs => {
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
    val res = f(x)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
    if (acc.contains(res)) distinctBy(xs, f, acc)  
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
    else x::distinctBy(xs, f, res::acc)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  }
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
} 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    65
// distinctBy  with the identity function is 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    66
// just distinct
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    67
distinctBy(ls, (x: Int) => x)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    68
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    69
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    70
val cs = List('A', 'b', 'a', 'c', 'B', 'D', 'd')
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    71
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    72
distinctBy(cs, (c:Char) => c.toUpper)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    73
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    74
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    75
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    76
// Type inference is local in Scala
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    77
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    78
def id[T](x: T) : T = x
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    79
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    80
val x = id(322)          // Int
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    81
val y = id("hey")        // String
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    82
val z = id(Set[Int](1,2,3,4)) // Set[Int]
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    83
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    84
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    85
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    86
// The type variable concept in Scala can get really complicated.
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    87
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    88
// - variance (OO)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    89
// - bounds (subtyping)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    90
// - quantification
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    91
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    92
// Java has issues with this too: Java allows
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    93
// to write the following incorrect code, and
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    94
// only recovers by raising an exception
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    95
// at runtime.
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    96
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    97
// Object[] arr = new Integer[10];
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
    98
// arr[0] = "Hello World";
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    99
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   100
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   101
// Scala gives you a compile-time error, which
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   102
// is much better.
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   103
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   104
var arr = Array[Int]()
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   105
arr(0) = "Hello World"
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   106
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   107
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   108
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   109
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   110
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   111
// Object Oriented Programming in Scala
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   112
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   113
// =====================================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   114
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   115
abstract class Animal
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   116
case class Bird(name: String) extends Animal {
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   117
   override def toString = name
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   118
}
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   119
case class Mammal(name: String) extends Animal
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   120
case class Reptile(name: String) extends Animal
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   121
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   122
Bird("Sparrow")
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   123
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   124
println(Bird("Sparrow"))
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   125
println(Bird("Sparrow").toString)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   126
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   127
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   128
// you can override methods
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   129
case class Bird(name: String) extends Animal {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   130
  override def toString = name
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   131
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   132
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   133
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   134
// There is a very convenient short-hand notation
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   135
// for constructors:
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   136
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   137
class Fraction(x: Int, y: Int) {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   138
  def numer = x
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   139
  def denom = y
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   140
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   141
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   142
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   143
case class Fraction(numer: Int, denom: Int)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   144
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   145
val half = Fraction(1, 2)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   146
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   147
half.denom
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   148
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   149
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   150
// In mandelbrot.scala I used complex (imaginary) numbers 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   151
// and implemented the usual arithmetic operations for complex 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   152
// numbers.
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   153
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   154
case class Complex(re: Double, im: Double) { 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   155
  // represents the complex number re + im * i
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   156
  def +(that: Complex) = Complex(this.re + that.re, this.im + that.im)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   157
  def -(that: Complex) = Complex(this.re - that.re, this.im - that.im)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   158
  def *(that: Complex) = Complex(this.re * that.re - this.im * that.im,
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   159
                                 this.re * that.im + that.re * this.im)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   160
  def *(that: Double) = Complex(this.re * that, this.im * that)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   161
  def abs = Math.sqrt(this.re * this.re + this.im * this.im)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   162
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   163
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   164
val test = Complex(1, 2) + Complex (3, 4)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   165
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   166
// this could have equally been written as
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   167
val test = Complex(1, 2).+(Complex (3, 4))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   168
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   169
// this applies to all methods, but requires
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   170
import scala.language.postfixOps
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   171
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   172
List(5, 2, 3, 4).sorted
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   173
List(5, 2, 3, 4) sorted
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   174
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   175
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   176
// ...to allow the notation n + m * i
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   177
import scala.language.implicitConversions   
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   178
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   179
val i = Complex(0, 1)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   180
implicit def double2complex(re: Double) = Complex(re, 0)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   181
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   182
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   183
val inum1 = -2.0 + -1.5 * i
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   184
val inum2 =  1.0 +  1.5 * i
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   185
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   186
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   187
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   188
// All is public by default....so no public is needed.
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   189
// You can have the usual restrictions about private 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   190
// values and methods, if you are MUTABLE !!!
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   191
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   192
case class BankAccount(init: Int) {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   193
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   194
  private var balance = init
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   195
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   196
  def deposit(amount: Int): Unit = {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   197
    if (amount > 0) balance = balance + amount
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   198
  }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   199
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   200
  def withdraw(amount: Int): Int =
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   201
    if (0 < amount && amount <= balance) {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   202
      balance = balance - amount
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   203
      balance
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   204
    } else throw new Error("insufficient funds")
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   205
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   206
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   207
// BUT since we are completely IMMUTABLE, this is 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   208
// virtually of not concern to us.
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   209
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   210
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   211
243
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   212
// another example about Fractions
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   213
import scala.language.implicitConversions
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   214
import scala.language.reflectiveCalls
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   215
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   216
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   217
case class Fraction(numer: Int, denom: Int) {
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   218
  override def toString = numer.toString + "/" + denom.toString
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   219
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   220
  def +(other: Fraction) = Fraction(numer + other.numer, denom + other.denom)
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   221
  def /(other: Fraction) = Fraction(numer * other.denom, denom * other.numer)
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   222
  def /% (other: Fraction) = Fraction(numer * other.denom, denom * other.numer)
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   223
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   224
}
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   225
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   226
implicit def Int2Fraction(x: Int) = Fraction(x, 1)
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   227
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   228
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   229
val half = Fraction(1, 2)
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   230
val third = Fraction (1, 3)
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   231
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   232
half + third
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   233
half / third
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   234
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   235
// not sure if one can get this to work
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   236
// properly, since Scala just cannot find out
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   237
// if / is for ints or for Fractions 
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   238
(1 / 3) + half
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   239
(1 / 2) + third
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   240
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   241
// either you have to force the Fraction-type by
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   242
// using a method that is not defined for ints
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   243
(1 /% 3) + half
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   244
(1 /% 2) + third
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   245
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   246
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   247
// ...or explicitly give the type in order to allow
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   248
// Scala to do the conversion to Fractions 
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   249
((1:Fraction) / 3) + half
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   250
(1 / (3: Fraction)) + half
9bb36426c781 updated
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
   251
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   252
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   253
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   254
// DFAs in Scala  
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   255
//===============
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   256
import scala.util.Try
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   259
// A is the state type
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   260
// C is the input (usually characters)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   261
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   262
case class DFA[A, C](start: A,              // starting state
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   263
                     delta: (A, C) => A,    // transition function
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   264
                     fins:  A => Boolean) { // final states (Set)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   265
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   266
  def deltas(q: A, s: List[C]) : A = s match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   267
    case Nil => q
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   268
    case c::cs => deltas(delta(q, c), cs)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   269
  }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   270
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   271
  def accepts(s: List[C]) : Boolean = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   272
    Try(fins(deltas(start, s))) getOrElse false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   273
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   274
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   275
// the example shown in the handout 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   276
abstract class State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   277
case object Q0 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   278
case object Q1 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   279
case object Q2 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   280
case object Q3 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   281
case object Q4 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   282
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   283
val delta : (State, Char) => State = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   284
  { case (Q0, 'a') => Q1
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   285
    case (Q0, 'b') => Q2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   286
    case (Q1, 'a') => Q4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   287
    case (Q1, 'b') => Q2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   288
    case (Q2, 'a') => Q3
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   289
    case (Q2, 'b') => Q2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   290
    case (Q3, 'a') => Q4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   291
    case (Q3, 'b') => Q0
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   292
    case (Q4, 'a') => Q4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   293
    case (Q4, 'b') => Q4 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   294
    case _ => throw new Exception("Undefined") }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   295
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   296
val dfa = DFA(Q0, delta, Set[State](Q4))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   297
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   298
dfa.accepts("abaaa".toList)     // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   299
dfa.accepts("bbabaab".toList)   // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   300
dfa.accepts("baba".toList)      // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   301
dfa.accepts("abc".toList)       // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   302
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   303
// another DFA with a Sink state
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   304
abstract class S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   305
case object S0 extends S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   306
case object S1 extends S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   307
case object S2 extends S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   308
case object Sink extends S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   309
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   310
// transition function with a sink state
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   311
val sigma : (S, Char) => S = 
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   312
  { case (S0, 'a') => S1
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   313
    case (S1, 'a') => S2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   314
    case _ => Sink
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   315
  }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   316
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   317
val dfa2 = DFA(S0, sigma, Set[S](S2))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   318
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   319
dfa2.accepts("aa".toList)        // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   320
dfa2.accepts("".toList)          // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   321
dfa2.accepts("ab".toList)        // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   322
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   323
//  we could also have a dfa for numbers
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   324
val sigmai : (S, Int) => S = 
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   325
  { case (S0, 1) => S1
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   326
    case (S1, 1) => S2
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   327
    case _ => Sink
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   328
  }
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   329
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   330
val dfa3 = DFA(S0, sigmai, Set[S](S2))
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   331
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   332
dfa3.accepts(List(1, 1))        // true
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   333
dfa3.accepts(Nil)               // false
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   334
dfa3.accepts(List(1, 2))        // false
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   335
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   336
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   337
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   338
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   339
// NFAs (Nondeterministic Finite Automata)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   340
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   341
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   342
case class NFA[A, C](starts: Set[A],          // starting states
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   343
                     delta: (A, C) => Set[A], // transition function
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   344
                     fins:  A => Boolean) {   // final states 
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   345
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   346
  // given a state and a character, what is the set of 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   347
  // next states? if there is none => empty set
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   348
  def next(q: A, c: C) : Set[A] = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   349
    Try(delta(q, c)) getOrElse Set[A]() 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   350
242
e6b34f617915 updated
Christian Urban <urbanc@in.tum.de>
parents: 226
diff changeset
   351
  def nexts(qs: Set[A], c: C) : Set[A] =
e6b34f617915 updated
Christian Urban <urbanc@in.tum.de>
parents: 226
diff changeset
   352
    qs.flatMap(next(_, c))
e6b34f617915 updated
Christian Urban <urbanc@in.tum.de>
parents: 226
diff changeset
   353
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   354
  // depth-first version of accepts
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   355
  def search(q: A, s: List[C]) : Boolean = s match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   356
    case Nil => fins(q)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   357
    case c::cs => next(q, c).exists(search(_, cs))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   358
  }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   359
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   360
  def accepts(s: List[C]) : Boolean =
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   361
    starts.exists(search(_, s))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   362
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   363
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   364
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   365
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   366
// NFA examples
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   367
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   368
val nfa_trans1 : (State, Char) => Set[State] = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   369
  { case (Q0, 'a') => Set(Q0, Q1) 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   370
    case (Q0, 'b') => Set(Q2) 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   371
    case (Q1, 'a') => Set(Q1) 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   372
    case (Q2, 'b') => Set(Q2) }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   373
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   374
val nfa = NFA(Set[State](Q0), nfa_trans1, Set[State](Q2))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   375
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   376
nfa.accepts("aa".toList)             // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   377
nfa.accepts("aaaaa".toList)          // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   378
nfa.accepts("aaaaab".toList)         // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   379
nfa.accepts("aaaaabbb".toList)       // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   380
nfa.accepts("aaaaabbbaaa".toList)    // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   381
nfa.accepts("ac".toList)             // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   382
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   383
223
c6453f3547ec updated
Christian Urban <urbanc@in.tum.de>
parents: 222
diff changeset
   384
// Q: Why the kerfuffle about the polymorphic types in DFAs/NFAs?
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   385
// A: Subset construction. Here the state type for the DFA is
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   386
//    sets of states.
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   387
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   388
def subset[A, C](nfa: NFA[A, C]) : DFA[Set[A], C] = {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   389
  DFA(nfa.starts, 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   390
      { case (qs, c) => nfa.nexts(qs, c) }, 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   391
      _.exists(nfa.fins))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   392
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   393
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   394
subset(nfa1).accepts("aa".toList)             // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   395
subset(nfa1).accepts("aaaaa".toList)          // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   396
subset(nfa1).accepts("aaaaab".toList)         // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   397
subset(nfa1).accepts("aaaaabbb".toList)       // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   398
subset(nfa1).accepts("aaaaabbbaaa".toList)    // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   399
subset(nfa1).accepts("ac".toList)             // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   400
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   401
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   402
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   403
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   404
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   405
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   406
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   407
// Cool Stuff in Scala
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   408
//=====================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   409
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   410
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   411
// Implicits or How to Pimp my Library
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   412
//=====================================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   413
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   414
// For example adding your own methods to Strings:
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   415
// Imagine you want to increment strings, like
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   416
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   417
//     "HAL".increment
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   418
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   419
// you can avoid ugly fudges, like a MyString, by
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   420
// using implicit conversions.
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   421
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   422
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   423
implicit class MyString(s: String) {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   424
  def increment = for (c <- s) yield (c + 1).toChar 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   425
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   426
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   427
"HAL".increment
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   428
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   429
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   430
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   431
// Regular expressions - the power of DSLs in Scala
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   432
//==================================================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   433
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   434
abstract class Rexp
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   435
case object ZERO extends Rexp                     // nothing
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   436
case object ONE extends Rexp                      // the empty string
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   437
case class CHAR(c: Char) extends Rexp             // a character c
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   438
case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative  r1 + r2
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   439
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence     r1 . r2  
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   440
case class STAR(r: Rexp) extends Rexp             // star         r*
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   441
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   442
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   443
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   444
// writing (ab)* in the format above is 
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   445
// tedious
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   446
val r0 = STAR(SEQ(CHAR('a'), CHAR('b')))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   447
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   448
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   449
// some convenience for typing in regular expressions
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   450
import scala.language.implicitConversions    
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   451
import scala.language.reflectiveCalls 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   452
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   453
def charlist2rexp(s: List[Char]): Rexp = s match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   454
  case Nil => ONE
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   455
  case c::Nil => CHAR(c)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   456
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   457
}
224
42d760984496 updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   458
implicit def string2rexp(s: String): Rexp = 
42d760984496 updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   459
  charlist2rexp(s.toList)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   460
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   461
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   462
val r1 = STAR("ab")
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   463
val r2 = STAR(ALT("ab", "baa baa black sheep"))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   464
val r3 = STAR(SEQ("ab", ALT("a", "b")))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   465
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   466
implicit def RexpOps (r: Rexp) = new {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   467
  def | (s: Rexp) = ALT(r, s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   468
  def % = STAR(r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   469
  def ~ (s: Rexp) = SEQ(r, s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   470
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   471
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   472
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   473
implicit def stringOps (s: String) = new {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   474
  def | (r: Rexp) = ALT(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   475
  def | (r: String) = ALT(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   476
  def % = STAR(s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   477
  def ~ (r: Rexp) = SEQ(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   478
  def ~ (r: String) = SEQ(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   479
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   480
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   481
//example regular expressions
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   482
val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   483
val sign = "+" | "-" | ""
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   484
val number = sign ~ digit ~ digit.% 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   485
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   486
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   487
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   488
// Lazy Evaluation
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   489
//=================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   490
//
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   491
// Do not evaluate arguments just yet:
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   492
// this uses the => in front of the type
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   493
// of the code-argument
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   494
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   495
def time_needed[T](i: Int, code: => T) = {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   496
  val start = System.nanoTime()
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   497
  for (j <- 1 to i) code
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   498
  val end = System.nanoTime()
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   499
  (end - start)/(i * 1.0e9)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   500
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   501
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   502
// same examples using the internal regexes
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   503
val evil = "(a*)*b"
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   504
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   505
("a" * 10 ++ "b").matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   506
("a" * 10).matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   507
("a" * 10000).matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   508
("a" * 20000).matches(evil)
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   509
("a" * 50000).matches(evil)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   510
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   511
time_needed(1, ("a" * 50000).matches(evil))