progs/re2.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 08 Nov 2021 01:16:13 +0000
changeset 402 de59aa20a1dc
parent 78 85f2f75abeeb
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
     1
// Part 2 about Regular Expression Matching
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
     2
//==========================================
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
     4
// copy over all code from re.scala
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
     5
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
     6
// (2a) Complete the function iterT which needs to
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
     7
// be tail-recursive(!) and takes an integer n, a 
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
     8
// function f and an x as arguments. This function 
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
     9
// should iterate f n-times starting with the 
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    10
// argument x.
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    11
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    12
import scala.annotation.tailrec
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    13
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    14
@tailrec
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    15
def iterT[A](n: Int, f: A => A, x: A): A = ...
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    16
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    17
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    18
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    19
// (2b) Complete the size function for regular
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    20
// expressions 
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    22
def size(r: Rexp): Int = ...
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    24
// two testcases about the sizes of simplified and 
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    25
// un-simplified derivatives
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    26
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    27
//val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    28
//size(iterT(20, (r: Rexp) => der('a', r), EVIL))        // should produce 7340068
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    29
//size(iterT(20, (r: Rexp) => simp(der('a', r)), EVIL))  // should produce 8
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    31
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    33
// (2c) Complete the fixpoint function below.
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    34
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    35
@tailrec
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    36
def fixpT[A](f: A => A, x: A): A = ...
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    39
/* testcases
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    40
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    41
//the Collatz function from CW 6 defined as fixpoint
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    42
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    43
def ctest(n: Long): Long =
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    44
  if (n == 1) 1 else
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    45
    if (n % 2 == 0) n / 2 else 3 * n + 1
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    46
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    47
// should all produce 1 
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    48
fixpT(ctest, 97L)
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    49
fixpT(ctest, 871L)
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    50
fixpT(ctest, 77031L)
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    51
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    52
*/
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    53
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    54
/*
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    55
// the same function on strings using the regular expression
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    56
// matcher
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    57
   
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    58
def foo(s: String): String = {
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    59
  if (matcher("a", s)) "a" else
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    60
  if (matcher("aa" ~ STAR("aa"), s)) s.take(s.length / 2) 
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    61
  else "a" ++ s * 3
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    64
// should all produce "a" 
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    65
fixpT(foo, "a" * 97)
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    66
fixpT(foo, "a" * 871)
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    67
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    68
*/