Exercise 4-6 Original
Exercise 4.6. Let expressions is derived expressions, because
(Let () ... ( )) )
is equivalent to
((Lambda (...)))
Implement a syntactic transformation let->combination that reduces evaluating let expressions to evaluating combination S of the type shown above, and add the appropriate clause to eval-handle let expressions.
Analysis
This problem requires that we convert let expressions into corresponding expressions.
(Let () ... ( )) )
From the above example we can see that in the let expression, it is assumed that it is expr, which can be obtained with CDR (() ... ()), and then use the high-order function map with car to remove all var.
Similarly, if you want to take out the exp part, use CADR first, then use the higher order function map with Cadr.
And the body part with CADDR can directly find out.
You can then start writing let->combination, which passes in a parameter expr.
((Lambda (...)))
According to this example code, we first call the No. 256 page of the book Make-lambda to construct the previous one, the function has two parameters parameter and body.
This will all be done, of course, and you will need to add the let to eval, before you define the lets. Also to invoke the No. 256 page of tagged-list?. So the next step is the specific code.
Code
(define (let-vars expr ) (map car (cadr expr))) (define (let-exp expr) (map cadr (cadr expr) ))(define (let-body expr ) (caddr expr))(define (let->combination expr)(cons (make-lambda ( let-vars expr ) ( let-body expr))(let-exp Expr )))(define (let. expr) (tagged-list expr ' let))(Let expr ) (eval (let->combination expr) env))
"SICP Exercise" 150 Exercise 4.6