progs/re2_sol.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 26 Aug 2020 02:07:46 +0100
changeset 340 9eeab89d0671
parent 107 22233a7c32d8
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
101
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
// Part 1 about Regular Expression Matching
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
//==========================================
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
abstract class Rexp
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
case object ZERO extends Rexp
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
case object ONE extends Rexp
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
case class CHAR(c: Char) extends Rexp
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
case class STAR(r: Rexp) extends Rexp 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
// some convenience for typing in regular expressions
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
import scala.language.implicitConversions    
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
import scala.language.reflectiveCalls 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
def charlist2rexp(s: List[Char]): Rexp = s match {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  case Nil => ONE
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  case c::Nil => CHAR(c)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
}
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
implicit def RexpOps (r: Rexp) = new {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
  def | (s: Rexp) = ALT(r, s)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  def % = STAR(r)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  def ~ (s: Rexp) = SEQ(r, s)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
}
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
implicit def stringOps (s: String) = new {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  def | (r: Rexp) = ALT(s, r)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  def | (r: String) = ALT(s, r)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  def % = STAR(s)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  def ~ (r: Rexp) = SEQ(s, r)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  def ~ (r: String) = SEQ(s, r)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
}
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
// (1a) Complete the function nullable according to
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
// the definition given in the coursework; this 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
// function checks whether a regular expression
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
// can match the empty string
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
def nullable (r: Rexp) : Boolean = r match {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  case ZERO => false
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  case ONE => true
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  case CHAR(_) => false
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  case STAR(_) => true
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
}
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
// (1b) Complete the function der according to
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
// the definition given in the coursework; this
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
// function calculates the derivative of a 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
// regular expression w.r.t. a character
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
def der (c: Char, r: Rexp) : Rexp = r match {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  case ZERO => ZERO
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
  case ONE => ZERO
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
  case CHAR(d) => if (c == d) ONE else ZERO
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  case SEQ(r1, r2) => 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
    else SEQ(der(c, r1), r2)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
}
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
// (1c) Complete the function der according to
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
// the specification given in the coursework; this
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
// function simplifies a regular expression;
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
// however it does not simplify inside STAR-regular
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
// expressions
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
def simp(r: Rexp) : Rexp = r match {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
  case ALT(r1, r2) => (simp(r1), simp(r2)) match {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
    case (ZERO, r2s) => r2s
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
    case (r1s, ZERO) => r1s
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
    case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
  }
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
    case (ZERO, _) => ZERO
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
    case (_, ZERO) => ZERO
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
    case (ONE, r2s) => r2s
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
    case (r1s, ONE) => r1s
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
    case (r1s, r2s) => SEQ(r1s, r2s)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
  }
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
  case r => r
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
}
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
// (1d) Complete the two functions below; the first 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
// calculates the derivative w.r.t. a string; the second
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
// is the regular expression matcher taking a regular
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
// expression and a string and checks whether the
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
// string matches the regular expression
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
def ders (s: List[Char], r: Rexp) : Rexp = s match {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
  case Nil => r
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  case c::s => ders(s, simp(der(c, r)))
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
}
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
// main matcher function
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
def matcher(r: Rexp, s: String): Boolean = nullable(ders(s.toList, r))
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
// (1e) Complete the function below: it searches (from the left to 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
// right) in string s1 all the non-empty substrings that match the 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
// regular expression -- these substrings are assumed to be
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
// the longest substrings matched by the regular expression and
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
// assumed to be non-overlapping. All these substrings in s1 are replaced
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
// by s2.
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
def splits(s: String): List[(String, String)] =
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
  (for (i <- (1 to s.length).toList) yield s.splitAt(i)).reverse
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
  
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
splits("abcde")
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
splits("")
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
def first(r: Rexp, lst: List[(String, String)]): Option[String] = lst match {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
  case Nil => None
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
  case (s1, s2)::xs => if (matcher(r, s1)) Some(s2) else first(r, xs)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
}
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
"abcd".head
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
def replace(r: Rexp, s1: String, s2: String): String = first(r, splits(s1)) match {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
  case None if (s1 == "") => ""
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
  case None => s1.head.toString ++ replace(r, s1.tail, s2)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
  case Some(s) => s2 ++ replace(r, s, s2) 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
}
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
val s1 =  "aabbbaaaaaaabaaaaabbaaaabb"
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
val r: Rexp = "aa".% | "bb"
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
splits(s1)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
first(r, splits(s1))
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
replace(r, s1, "c")
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
splits("bb")
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
first(r, splits("bb"))
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
replace(r, "abb", "c")
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
// PART 2
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
//========
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
// (2a)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
import scala.annotation.tailrec
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
@tailrec
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
def iterT[A](n: Int, f: A => A, x: A): A = 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
  if (n == 0) x else iterT(n - 1, f, f(x)) 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
//non-tail recursive iter
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
107
22233a7c32d8 updated
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   160
//def iter[A](n: Int, f: A => A)(x: A): A = 
22233a7c32d8 updated
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
   161
//  if (n == 0) x else f(iter(n - 1,f, x)) 
101
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
// (2b)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
def size(r: Rexp): Int = r match {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
  case ZERO => 1
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
  case ONE => 1
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
  case CHAR(_) => 1
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
  case ALT(r1, r2) => 1 + size(r1) + size (r2)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
  case SEQ(r1, r2) => 1 + size(r1) + size (r2)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
  case STAR(r1) => 1 + size(r1)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
}
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
size(iterT(20, (r: Rexp) => der('a', r), EVIL))        // should produce 7340068        
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
size(iterT(20, (r: Rexp) => simp(der('a', r)), EVIL))  // should produce 8
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
// (2c)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
@tailrec
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
def fixpT[A](f: A => A, x: A): A = {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
  val fx = f(x)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
  if (fx == x) x else fixpT(f, fx) 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
}
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
def ctest(n: Long): Long =
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
  if (n == 1) 1 else
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
    if (n % 2 == 0) n / 2 else 3 * n + 1
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
fixpT(ctest, 97L)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
fixpT(ctest, 871L)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
fixpT(ctest, 77031L)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
fixpT(ctest, 837799L)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
def foo(s: String): String = {
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
  if (matcher("a", s)) "a" else
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
  if (matcher("aa" ~ STAR("aa"), s)) s.take(s.length / 2) 
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
  else "a" ++ s * 3
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
}
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
fixpT(foo, "a" * 97)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
fixpT(foo, "a" * 871)
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
139eb1ed2d57 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208