progs/lecture4.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 30 Nov 2018 03:44:27 +0000
changeset 222 e52cc402caee
parent 218 22705d22c105
child 223 c6453f3547ec
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    11
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    39
map(List(1, 2, 3, 4), (x: Int) => x * x)
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    51
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
def distinctBy[B, C](xs: List[B], f: B => C, acc: List[C] = Nil): List[B] = xs match {
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  case Nil => Nil
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  case (x::xs) => {
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
    val res = f(x)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
    if (acc.contains(res)) distinctBy(xs, f, acc)  
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
    else x::distinctBy(xs, f, res::acc)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
  }
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
} 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    61
distinctBy(ls, (x: Int) => x)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    62
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    63
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    64
val cs = List('A', 'b', 'a', 'c', 'B', 'D', 'd')
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    65
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    66
distinctBy(cs, (c:Char) => c.toUpper)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    67
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
// Type inference is local in Scala
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
def id[T](x: T) : T = x
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
val x = id(322)          // Int
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    76
val y = id("hey")        // String
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    77
val z = id(Set(1,2,3,4)) // Set[Int]
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    78
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    81
// The type variable concept in Scala can get really complicated.
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    82
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    83
// - variance (OO)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    84
// - bounds (subtyping)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    85
// - quantification
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    86
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    87
// Java has issues with this too: Java allows
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    88
// to write the following, but raises an exception
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    89
// at runtime
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    90
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    91
//Object[] arr = new Integer[10];
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    92
//arr[0] = "Hello World";
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    93
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    94
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    95
// Scala gives you a compile-time error
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    96
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    97
var arr = Array[Int]()
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    98
arr(0) = "Hello World"
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   101
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   102
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   105
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   106
// Object Oriented Programming in Scala
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
abstract class Animal
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   111
case class Bird(name: String) extends Animal
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   112
case class Mammal(name: String) extends Animal
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   113
case class Reptile(name: String) extends Animal
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
println(new Bird("Sparrow"))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   116
println(Bird("Sparrow").toString)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   117
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   118
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   119
// you can override methods
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   120
case class Bird(name: String) extends Animal {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   121
  override def toString = name
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   122
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   123
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   124
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   125
// There is a very convenient short-hand notation
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   126
// for constructors
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
class Fraction(x: Int, y: Int) {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   129
  def numer = x
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   130
  def denom = y
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
case class Fraction(numer: Int, denom: Int)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   135
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   136
val half = Fraction(1, 2)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   137
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   138
half.denom
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   139
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
// in mandelbrot.scala I used complex (imaginary) numbers and implemented
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   142
// the usual arithmetic operations for complex numbers
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   143
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   144
case class Complex(re: Double, im: Double) { 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   145
  // represents the complex number re + im * i
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   146
  def +(that: Complex) = Complex(this.re + that.re, this.im + that.im)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   147
  def -(that: Complex) = Complex(this.re - that.re, this.im - that.im)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   148
  def *(that: Complex) = Complex(this.re * that.re - this.im * that.im,
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   149
                                 this.re * that.im + that.re * this.im)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   150
  def *(that: Double) = Complex(this.re * that, this.im * that)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   151
  def abs = Math.sqrt(this.re * this.re + this.im * this.im)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   152
}
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
val test = Complex(1, 2) + Complex (3, 4)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   155
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   156
// this could have equally been written as
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   157
val test = Complex(1, 2).+(Complex (3, 4))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   158
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   159
// this applies to all methods, but requires
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   160
import scala.language.postfixOps
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   161
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   162
List(5, 2, 3, 4).sorted
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   163
List(5, 2, 3, 4) sorted
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   164
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
// to allow the notation n + m * i
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   167
import scala.language.implicitConversions   
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   168
object i extends Complex(0, 1)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   169
implicit def double2complex(re: Double) = Complex(re, 0)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   170
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
val inum1 = -2.0 + -1.5 * i
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   173
val inum2 =  1.0 +  1.5 * i
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   176
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   177
// all is public by default....so no public
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   178
// you can have the usual restrictions about private values
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   179
// and methods, if you are MUTABLE(!!!)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   180
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   181
case class BankAccount(init: Int) {
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
  private var balance = init
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   184
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   185
  def deposit(amount: Int): Unit = {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   186
    if (amount > 0) balance = balance + amount
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   187
  }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   188
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   189
  def withdraw(amount: Int): Int =
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   190
    if (0 < amount && amount <= balance) {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   191
      balance = balance - amount
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   192
      balance
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   193
    } else throw new Error("insufficient funds")
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   194
}
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
// BUT since we are IMMUTABLE, this is virtually of not 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   197
// concern to us.
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   201
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   202
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   203
// DFAs in Scala  
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   204
import scala.util.Try
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   207
// A is the state type
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   208
// C is the input (usually characters)
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
case class DFA[A, C](start: A,               // starting state
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   211
                     delta: (A, C) => A,     // transition function
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   212
                     fins:  A => Boolean) {  // final states
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   213
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   214
  def deltas(q: A, s: List[C]) : A = s match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   215
    case Nil => q
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   216
    case c::cs => deltas(delta(q, c), cs)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   217
  }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   218
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   219
  def accepts(s: List[C]) : Boolean = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   220
    Try(fins(deltas(start, s))) getOrElse false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   221
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   223
// the example shown in the handout 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   224
abstract class State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   225
case object Q0 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   226
case object Q1 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   227
case object Q2 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   228
case object Q3 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   229
case object Q4 extends State
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   230
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   231
val delta : (State, Char) => State = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   232
  { case (Q0, 'a') => Q1
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   233
    case (Q0, 'b') => Q2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   234
    case (Q1, 'a') => Q4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   235
    case (Q1, 'b') => Q2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   236
    case (Q2, 'a') => Q3
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   237
    case (Q2, 'b') => Q2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   238
    case (Q3, 'a') => Q4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   239
    case (Q3, 'b') => Q0
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   240
    case (Q4, 'a') => Q4
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   241
    case (Q4, 'b') => Q4 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   242
    case _ => throw new Exception("Undefined") }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   243
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   244
val dfa = DFA(Q0, delta, Set[State](Q4))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   245
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   246
dfa.accepts("abaaa".toList)     // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   247
dfa.accepts("bbabaab".toList)   // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   248
dfa.accepts("baba".toList)      // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   249
dfa.accepts("abc".toList)       // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   250
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   251
// another DFA test with a Sink state
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   252
abstract class S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   253
case object S0 extends S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   254
case object S1 extends S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   255
case object S2 extends S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   256
case object Sink extends S
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   257
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   258
// transition function with a sink state
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   259
val sigma : (S, Char) :=> S = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   260
  { case (S0, 'a') => S1
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   261
    case (S1, 'a') => S2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   262
    case _ => Sink
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   263
  }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   264
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   265
val dfa2 = DFA(S0, sigma, Set[S](S2))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   266
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   267
dfa2.accepts("aa".toList)        // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   268
dfa2.accepts("".toList)          // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   269
dfa2.accepts("ab".toList)        // false
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   272
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
// NFAs (Nondeterministic Finite Automata)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   275
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   276
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   277
case class NFA[A, C](starts: Set[A],           // starting states
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   278
                     delta: (A, C) => Set[A],  // transition function
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   279
                     fins:  A => Boolean) {    // final states 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   280
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   281
  // given a state and a character, what is the set of 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   282
  // next states? if there is none => empty set
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   283
  def next(q: A, c: C) : Set[A] = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   284
    Try(delta(q, c)) getOrElse Set[A]() 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   285
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   286
  def nexts(qs: Set[A], c: C) : Set[A] =
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   287
    qs.flatMap(next(_, c))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   288
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   289
  // depth-first version of accepts
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   290
  def search(q: A, s: List[C]) : Boolean = s match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   291
    case Nil => fins(q)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   292
    case c::cs => next(q, c).exists(search(_, cs))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   293
  }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   294
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   295
  def accepts(s: List[C]) : Boolean =
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   296
    starts.exists(search(_, s))
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   299
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   300
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   301
// NFA examples
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   302
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   303
val nfa_trans1 : (State, Char) => Set[State] = 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   304
  { case (Q0, 'a') => Set(Q0, Q1) 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   305
    case (Q0, 'b') => Set(Q2) 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   306
    case (Q1, 'a') => Set(Q1) 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   307
    case (Q2, 'b') => Set(Q2) }
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   308
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   309
val nfa = NFA(Set[State](Q0), nfa_trans1, Set[State](Q2))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   310
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   311
nfa.accepts("aa".toList)             // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   312
nfa.accepts("aaaaa".toList)          // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   313
nfa.accepts("aaaaab".toList)         // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   314
nfa.accepts("aaaaabbb".toList)       // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   315
nfa.accepts("aaaaabbbaaa".toList)    // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   316
nfa.accepts("ac".toList)             // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   317
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
// Q: Why the kerfuffle about the polymorphic types in DFAs/NFAs
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   320
// A: Subset construction
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   321
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   322
def subset[A, C](nfa: NFA[A, C]) : DFA[Set[A], C] = {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   323
  DFA(nfa.starts, 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   324
      { case (qs, c) => nfa.nexts(qs, c) }, 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   325
      _.exists(nfa.fins))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   326
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   327
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   328
subset(nfa1).accepts("aa".toList)             // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   329
subset(nfa1).accepts("aaaaa".toList)          // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   330
subset(nfa1).accepts("aaaaab".toList)         // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   331
subset(nfa1).accepts("aaaaabbb".toList)       // true
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   332
subset(nfa1).accepts("aaaaabbbaaa".toList)    // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   333
subset(nfa1).accepts("ac".toList)             // false
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   334
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   335
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
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
// Cool Stuff in Scala
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   342
//=====================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   343
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   344
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   345
// Implicits or How to Pimp my Library
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   346
//=====================================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   347
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   348
// For example adding your own methods to Strings:
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   349
// Imagine you want to increment strings, like
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   350
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   351
//     "HAL".increment
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   352
//
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   353
// you can avoid ugly fudges, like a MyString, by
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   354
// using implicit conversions.
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   355
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   356
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   357
implicit class MyString(s: String) {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   358
  def increment = for (c <- s) yield (c + 1).toChar 
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   361
"HAL".increment
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
// Regular expressions - the power of DSLs in Scala
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   369
abstract class Rexp
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   370
case object ZERO extends Rexp                       // nothing
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   371
case object ONE extends Rexp                        // the empty string
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   372
case class CHAR(c: Char) extends Rexp               // a character c
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   373
case class ALT(r1: Rexp, r2: Rexp) extends Rexp     // alternative  r1 + r2
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   374
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp     // sequence     r1 . r2  
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   375
case class STAR(r: Rexp) extends Rexp               // star         r*
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   376
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   377
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   378
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   379
// (ab)*
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   380
val r0 = STAR(SEQ(CHAR('a'), CHAR('b')))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   381
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
// some convenience for typing in regular expressions
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   384
import scala.language.implicitConversions    
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   385
import scala.language.reflectiveCalls 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   386
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   387
def charlist2rexp(s: List[Char]): Rexp = s match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   388
  case Nil => ONE
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   389
  case c::Nil => CHAR(c)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   390
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   391
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   392
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   395
val r1 = STAR("ab")
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   396
val r2 = STAR(ALT("ab", "baa baa black sheep"))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   397
val r3 = STAR(SEQ("ab", ALT("a", "b")))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   398
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   399
implicit def RexpOps (r: Rexp) = new {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   400
  def | (s: Rexp) = ALT(r, s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   401
  def % = STAR(r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   402
  def ~ (s: Rexp) = SEQ(r, s)
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
implicit def stringOps (s: String) = new {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   406
  def | (r: Rexp) = ALT(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   407
  def | (r: String) = ALT(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   408
  def % = STAR(s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   409
  def ~ (r: Rexp) = SEQ(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   410
  def ~ (r: String) = SEQ(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   411
}
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
//example regular expressions
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   414
val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   415
val sign = "+" | "-" | ""
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   416
val number = sign ~ digit ~ digit.% 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   417
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   420
// Lazy Evaluation
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
// do not evaluate arguments just yet
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   424
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   425
def time_needed[T](i: Int, code: => T) = {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   426
  val start = System.nanoTime()
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   427
  for (j <- 1 to i) code
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   428
  val end = System.nanoTime()
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   429
  (end - start)/(i * 1.0e9)
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
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   432
// same examples using the internal regexes
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   433
val evil = "(a*)*b"
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   434
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   435
("a" * 10 ++ "b").matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   436
("a" * 10).matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   437
("a" * 10000).matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   438
("a" * 20000).matches(evil)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   439
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   440
time_needed(2, ("a" * 10000).matches(evil))