Exercise 4-7 Original
Exercise 4.7. Let* is similar to let, except that the bindings of the A let variables be performed sequentially from left to right, and E Ach binding is made in a environment in which all of the preceding bindings are visible. For example
(let* ( (x 3 ) (y (+ x 2 ) ) Span class= "Hljs-list" > (z (< Span class= "hljs-built_in" >+ x y 5 ) ) ) (* x z) )
Returns 39. Explain How a let* expression can is rewritten as a set of nested let expressions, and write a procedure let*->nested-l ETS that performs this transformation. If we have already implemented let (exercise 4.6) and we want to extend the evaluator to handle let*, are 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?
Analysis
This problem is similar to the previous, the main points in the grasp of the problem will be solved. That is to say from left to right evaluation, then we can use list and car and CDR to complete, the core idea is to use recursion, continuous to the right, until exp is empty, then return to body, and then end the construction. As for tagged-list? These are the same as the previous question.
Code
(define (let* expr) (tagged-list expr ' let*))(define (let*-body expr ) (caddr expr))(define (let*-exp expr ) (cadr expr))(Define (let*->nested-lets expr) ( Let ( (exp let*-exp expr) ) ( Body (let*-body expr) ) ) (defien (make-lets Exprs)(if (null? Exprs) body
(
list ' let
( car exprs))
(make-lets (cdr exprs
)))) (make-lets exp)))
"SICP Exercise" 151 Exercise 4.7