Title: 3'4 LETREC: A Language with Recursive Procedures
13.4 LETREC A Language with Recursive Procedures
Unlike letrec in Scheme, our toy-language letrec
will be used only for recursive procedures
letrec double(x) if zero?(x) then 0 else
((double (x,1)), -2) in (double 6)
2- Syntax
- ltexpressiongt letrec ltidentifiergt
(ltidentifiergt) - ltexpressiongt in ltexpressiongt
- As usual, we will have to define some new
datatypes and functions.
3 (define-datatype environment environment? (empty
-env) (extend-env) (var identifier?) (val
expval?) (env environment)) (extend-env-rec (
p-name identifier?) (b-var identifier?) (body
expression?) (env environment?)))
4 (define extend-env-rec (lambda (p-name b-var
body saved-env) (let ((vec (make-vector 1)))
(let ((new-env (extend-env
p-name vec saved-env))) (vector-set! vec
0 do this, then ... (proc-val
(procedure b-var body new-env)))
new-env)))) return this
Does something seem fishy about this?
5 (let ((new-env (extend-env
p-name vec saved-env))) (vector-set! vec
0 do this, then ... (proc-val
(procedure b-var body new-env)))
new-env
p-name
vec
saved-env
b-var
body
6letrec double(x) if zero?(x) then 0 else
((double (x,1)), -2) in (double 6)
double
if zero? (x) then 0 else ...
x
7 (define apply-env (lambda (env search-var)
(cases environment env (empty-env ()
(eoplerror You suck at Photoshop))
(extend-env (saved-var saved-val saved-env)
(if (eqv? saved-var search-var)
saved-val (apply-env saved-env
search-var))) (extend-env-rec (p-name b-var
p-body saved-env) (if (eqv? search-var
p-name) (proc-val (procedure b-var
p-body env)) (apply-env saved-env
search-var))))))