Exercise 4.7. Let* is similar to let, except that the bindings of the let variables are performed sequentially from left to right, and each binding is made in an environment in which all of the preceding bindings are visible. For example (let* ((x 3) (y (+ x 2)) (z (+ x y 5))) (* x z)) returns 39. Explain how a let* expression can be rewritten as a set of nested let expressions, and write a procedure let*->nested-lets that performs this transformation. If we have already implemented let (exercise 4.6) and we want to extend the evaluator to handle let*, is it sufficient to add a clause to eval whose action is (eval (let*->nested-lets exp) env) or must we explicitly expand let* in terms of non-derived expressions? ———————————————————————————————————————————————————————————————————————— A let* expression can be expanded into nested lets such that each let binds only one variable. This ensures that any later binding which uses the earlier binding will be able to see it, e.g. the above can be written as: (let ((x 3)) (let ((y (+ x 2))) (let ((z (+ x y 5))) (* x z)))) So: (define (let*? exp) (tagged-list? exp 'let*)) (define (let*-bindings exp) (cadr exp)) (define (let*-body exp) (caddr exp)) ;; as noted in meeting, this doesn't handle multiple expressions in the let body (define (let*->nested-lets exp) (nested-lets (let*-bindings exp) (let*-body exp))) (define (nested-lets bindings body) (if (null? bindings) body (make-let (list (car bindings)) (nested-lets (cdr bindings) body)))) (define (make-let bindings body) (list 'let bindings body) Using let*->nested-lets as shown is sufficient, as the eval will recognize the let on the recursive call and handle it correctly.