progs/dfa.scala
changeset 483 6f508bcdaa30
parent 481 acd8780bfc8b
child 485 bd6d999ab7b6
equal deleted inserted replaced
482:0f6e3c5a1751 483:6f508bcdaa30
     2 import scala.util.Try
     2 import scala.util.Try
     3 
     3 
     4 // type abbreviation for partial functions
     4 // type abbreviation for partial functions
     5 type :=>[A, B] = PartialFunction[A, B]
     5 type :=>[A, B] = PartialFunction[A, B]
     6 
     6 
     7 case class DFA[A, C](start: A,                // starting state
     7 case class DFA[A, C](start: A,               // starting state
     8                      delta: (A, C) :=> A,     // transition partial fun
     8                      delta: (A, C) :=> A,    // transition (partial fun)
     9                      fins:  A => Boolean) {   // final states
     9                      fins:  A => Boolean) {  // final states
    10 
    10 
    11   def deltas(q: A, s: List[C]) : A = s match {
    11   def deltas(q: A, s: List[C]) : A = s match {
    12     case Nil => q
    12     case Nil => q
    13     case c::cs => deltas(delta(q, c), cs)
    13     case c::cs => deltas(delta(q, c), cs)
    14   }
    14   }