main_solution3/re.scala
changeset 475 59e005dcf163
parent 470 86a456f8cb92
--- a/main_solution3/re.scala	Thu Nov 02 13:53:37 2023 +0000
+++ b/main_solution3/re.scala	Thu Nov 02 23:34:53 2023 +0000
@@ -3,7 +3,6 @@
 
 object M3 {
 
-// Regular Expressions
 abstract class Rexp
 case object ZERO extends Rexp
 case object ONE extends Rexp
@@ -18,30 +17,38 @@
 def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
 def SEQ(r1: Rexp, r2: Rexp) = SEQs(List(r1, r2))
 
-// some convenience for typing in regular expressions
-import scala.language.implicitConversions    
-import scala.language.reflectiveCalls 
+
+// some convenience for typing regular expressions
 
 def charlist2rexp(s: List[Char]): Rexp = s match {
   case Nil => ONE
   case c::Nil => CHAR(c)
   case c::s => SEQ(CHAR(c), charlist2rexp(s))
 }
-implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
+
+import scala.language.implicitConversions
 
-implicit def RexpOps (r: Rexp) = new {
+given Conversion[String, Rexp] = (s => charlist2rexp(s.toList))
+
+extension (r: Rexp) {
   def | (s: Rexp) = ALT(r, s)
   def % = STAR(r)
   def ~ (s: Rexp) = SEQ(r, s)
 }
 
-implicit def stringOps (s: String) = new {
-  def | (r: Rexp) = ALT(s, r)
-  def | (r: String) = ALT(s, r)
-  def % = STAR(s)
-  def ~ (r: Rexp) = SEQ(s, r)
-  def ~ (r: String) = SEQ(s, r)
-}
+// some examples for the conversion and extension:
+
+// val areg : Rexp = "a" | "b"
+//  => ALTs(List(CHAR('a'), CHAR('b')))
+//
+// val sreg : Rexp = "a" ~ "b"
+//  => SEQs(List(CHAR('a'), CHAR('b'))) 
+//
+// val star_reg : Rexp = ("a" ~ "b").%
+//  => STAR(SEQs(List(CHAR('a'), CHAR('b')))) 
+
+// ADD YOUR CODE BELOW
+//======================
 
 // (1) 
 def nullable (r: Rexp) : Boolean = r match {
@@ -194,8 +201,9 @@
 
 
 
-/*
-// if nullable(r1)  
-  ALTs(SEQs(der(c, r1)::rs)::(rs filter what is nullable) .map(der(c,_)))
 
-*/
+// This template code is subject to copyright 
+// by King's College London, 2022. Do not 
+// make the template code public in any shape 
+// or form, and do not exchange it with other 
+// students under any circumstance.