Week 02 featured the following tagged and fully parenthesized syntactic representation of regular expressions:
Week 11 featured an interpreter for this representation of regular expressions: a Scheme procedure that, given a regular expression and a proper list of integers, tests whether this list belongs to the set represented by this regular expression.
Week 12, in this chapter, features compiled regular expressions: Scheme procedures that, given a proper list of integers, test whether this list belongs to the regular expression that was compiled.
So for example, given a regular expression re, the challenge is to implement a Scheme procedure c_re such that, for any proper list of integers ns applying the interpreter to re and ns gives the same result as applying c_re to ns. Here is the relation between re and c_re:
for all ns, (match re ns) == (c_re ns)
where match denotes the interpreter for regular expressions features in Week 11. In words, matching re and ns and applying c_re to ns should give the same result.
Pablito: Well, given re, c_re is not difficult to implement, is it?
Bong-sun: Pray elaborate, Pablito.
Pablito: It can just call match, look:
(define c_re
(lambda (ns)
(match re ns)))
Bong-sun: This implementation is definitely correct, but then c_re is not a compiled version of re, is it?
Pablito: Right. Well, it can be useful in the unit tests.
Bong-sun: That is can.
Given the following regular expressions, implement their compiled counterpart:
Here is a template for your solutions:
(define re1
'(mt))
(define test_c_re1
(lambda (candidate)
(and (candidate '())
(not (candidate '(10)))
(not (candidate '(10 20)))
(not (candidate '(10 20 30))))))
(define c_re1
(lambda (ns)
(null? ns)))
(unless (test_c_re1 c_re1)
(printf "c_re1 failed its unit test"))
;;;;;
(define re2
'(atom 10))
(define test_c_re2
(lambda (candidate)
(and (candidate '(10))
(not (candidate '()))
(not (candidate '(10 20)))
(not (candidate '(10 20 30))))))
(define c_re2
(lambda (ns)
...))
(unless (test_c_re2 c_re2)
(printf "c_re2 failed its unit test"))
;;;;;
(define re3
'(seq (atom 10) (atom 20)))
;;;;;
(define re4
'(seq (atom 10) (seq (atom 20) (atom 30))))
Food for thought:
The goal of this exercise is to compile regular expressions that involve star.
So given the following regular expressions, implement their compiled counterpart:
Hint: Use letrec.
Bonus if you can compile a regular expression that contains two nested occurrences of star.
The goal of this exercise is to compile regular expressions that involve star.
So given the following regular expressions, implement their compiled counterpart:
Hint: Use letrec.
Bonus if you can compile a regular expression that contains two nested occurrences of star.
Created [08 Nov 2025]