--- a/progs/matcher/re1.sc Thu Oct 02 08:46:35 2025 +0100
+++ b/progs/matcher/re1.sc Fri Oct 03 10:10:33 2025 +0100
@@ -9,7 +9,7 @@
// amm re1.sc all
//
-
+
// regular expressions (as enum in Scala 3)
enum Rexp {
case ZERO // matches nothing
@@ -21,6 +21,20 @@
}
import Rexp._
+
+/*
+UPDATE: The videos and handouts still us the older syntax
+with classes, which still works but is more verbose
+
+abstract class Rexp
+case object ZERO extends Rexp
+case object ONE extends Rexp
+case class CHAR(c: Char) extends Rexp
+case class ALT(r1: Rexp, r2: Rexp) extends Rexp
+case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
+case class STAR(r: Rexp) extends Rexp
+*/
+
// nullable function: tests whether a regular
// expression can recognise the empty string
def nullable(r: Rexp) : Boolean = r match {
@@ -217,30 +231,3 @@
@main
def all() = { test1(); test2() ; test3() ; test4() }
-
-
-
-
-// partial derivatives produce a set of regular expressions
-def pder(c: Char, r: Rexp) : Set[Rexp] = r match {
- case ZERO => Set()
- case ONE => Set()
- case CHAR(d) => if (c == d) Set(ONE) else Set()
- case ALT(r1, r2) => pder(c, r1) ++ pder(c, r2)
- case SEQ(r1, r2) => {
- (for (pr1 <- pder(c, r1)) yield SEQ(pr1, r2)) ++
- (if (nullable(r1)) pder(c, r2) else Set())
- }
- case STAR(r1) => {
- for (pr1 <- pder(c, r1)) yield SEQ(pr1, STAR(r1))
- }
-}
-
-def pders(s: List[Char], rs: Set[Rexp]) : Set[Rexp] = s match {
- case Nil => rs
- case c::s => pders(s, rs.flatMap(pder(c, _)))
-}
-
-def pders1(s: String, r: Rexp) = pders(s.toList, Set(r))
-
-