"SICP Exercise" 152 Exercise 4.8

Source: Internet
Author: User

Exercise 4-8 Original

Exercise 4.8. "Named let" are a variant of let that have the form

<var><bindings><body>)

The and is just as in ordinary let, except that is bound within to a procedure whose body is and whose parameters are the variables in the . Thus, one can repeatedly execute the by invoking the procedure named . For example, the iterative Fibonacci procedure (section 1.2.2) can be rewritten using named let as follows:

(Define (fib N)    ( LetFib-iter((a  1)(b  0)(count N))                                                  (if (= count 0) b (fib-iter (+ a B) a (- count 1)))))

Modify Let->combination of Exercise 4.6 to also support named Let.

Analysis

I hope we still have something to look at the original text, I have seen many times the modify original is the meaning of the change.

About named let's some comparison of what, you can see here: "Scheme induction" 3 compare do, allow, loop.

From the code of the topic, we can also see that Named-let's name can be taken out by Cadr, which is the Fib-iter in the book. The body section can also be seen with 3 Cdr and a car from the code below.

<var><bindings><body>)

The parameter problem has been said to be binding in the variable, remove the binding with CADDR, and take out the topic example of a, B and count, etc. are used map and car. Remove 1, 0, and N from the topic example using Map and Cadr.

Then we also need to convert named-let into functions, with list to construct these, first of all, of course, ' Define, and then use cons to name and parameter together, and finally the body.

Of course, in the let->combination we need to judge is not Named-let?, then how to judge, first judge whether it is let, and then judge the name of expr is not the symbol (symbol?).

Finally, you can write let-combination. First use the written named-let predicate to judge expr, and then invoke the No. 257 page of the SEQUENCE->EXP function for real, otherwise use cons to continue the construction.

Code
(define (named-let-name expr)(cadr expr))   (define (named-let-body expr)(cadddr expr))  (define (named-let-parameters expr)(map car (caddr expr   )))(define (named-let-exp expr)(map cadr (caddr expr)) )  (define (named-let. expr)( and ( let) expr)  (symbol? ) (cadr expr   ))))  (define (named-let->func expr)(list ' define (cons  (named-let-name EPXR) (named-let-parameters expr))           (named-let-body expr       )))(Define (let->combination expr)  (if (named-let. expr)      (sequence->exp(list (named-let->func expr)(cons (named-let-name expr) (named-let-exp expr         ))))      (cons (make-lambda (let-vars expr)(list (  Let-body expr )))(let-exp expr         ))))

"SICP Exercise" 152 Exercise 4.8

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.