C311 Assignment 6 -- Recursion and Scope

Due Monday, February 19, at 9 AM

Submit via email

Use the interpreter from the previous assigment for this exercise.
  1. Letrec
    Use Scheme syntax and semantics to implement letrec as an extension to your interpreter's functionality (and not as a macro expansion exercise). Add this functionality by making the letrec form behave like the syntax expansion method (which uses let and set!), but create the environments and mutate them yourself. Save this version of the interpreter for future use.
    > (run '(define ev 
    	  (lambda (n)
    	    (letrec 
    	      ((even
    		 (lambda (n)
    		   (if (zero n)
    		       1
    		       (odd (sub1 n)))))
    	       (odd
    		 (lambda (n)
    		   (if (zero n)
    		       0
    		       (even (sub1 n))))))
    	      (even n)))))
    > (run '(ev 10))
    1
    > (run '(ev 9))
    0
    
  2. Scope
    Extend your interpreter with a dynlet form
    (dynlet ((var exp) ...) body)
    that dynamically binds its variables, which belong to a separate dynvar namespace. Also provide a dynamic variable reference form, with syntax (dyn var).

    Hint: Use two environments

    Some further description of dynlet (with examples) is available.

    Note that if every procedure is of the form

    (lambda (v ...) (dynlet ((v v) ...) body))
    and only dyn variable references are used in the body, then one gets the semantics of the dynamic binding interpreter in the book.

Submission

Write your answers to the exercises in a file (with comments, following the proper indentation rules), and send that file to

c311@lakshmi.cs.indiana.edu
with the subject line
6
Assuming you've saved your homework in the file ``asgn.ss'' in the current directory, one way to submit is with the command:
Mail -s "6" c311@lakshmi.cs.indiana.edu < asgn.ss
If you use the define-record stuff, include the expression
(load "record.ss")
in your submission.

Back to the c311 page

ehilsdal@cs.indiana.edu