"SICP Exercise" 151 Exercise 4.7

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.