3'4 LETREC: A Language with Recursive Procedures - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

3'4 LETREC: A Language with Recursive Procedures

Description:

... (vec (make-vector 1))) (let ((new-env (extend-env p-name vec ... vec 0 ; do this, then ... (proc-val (procedure b-var body new-env))) new-env)))) ; return this ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 8
Provided by: csW7
Category:

less

Transcript and Presenter's Notes

Title: 3'4 LETREC: A Language with Recursive Procedures


1
3.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

6
letrec 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))))))
Write a Comment
User Comments (0)
About PowerShow.com