Summary of problems solved by SiC exercises (1.41)

Source: Internet
Author: User

The source question 1.41 does not seem to have any relationship with the surrounding questions. Suddenly, we were asked to define a process called double. In fact, the core of this question is the high-level function.


The question requires us to define a process double, which uses a process as a parameter. This process as a parameter has been agreed to be a single-parameter process. The double process needs to return a process. The returned process applies the passed process twice.


For example, if we have a process called (slap in the face), the process will be slapped.

Then (double slap) will return another process, which has no name. For now, let's call it "slap the two) it will make two slashes.

That is to say, a call like this will cause two slashes.


Well, the question is (double) INC) 5) What is the result, where the INC method will add 1 to the input parameter.


To complete this question, let's take a look at how to define double.


The double type is defined as follows:

(define (double f)  (lambda (x)     (f (f x)))) 

To test, I defined an INC process of my own.

(define (my-inc x)  (+ x 1))

Final direct test

(define test-it (((double ( double double)) my-inc) 5))

The result is 21, that is, 5 + 16, that is, 16 times plus one operation.


Why?


We can expand it step by step


First, we need to set different double labels, namely, double1, double2, and double3. This is clear (define Step1 (double1 (double2 double3) My-Inc) 5 )); then expand (double2 double3): (define step2 (double1 (lambda (x) (double3 (double3 X) My-Inc) 5 )); name (lambda (x) (double3 (double3 X) as lam1 :( define (lam1 X) (double3 (double3 X ))); in this way, step 2 is equivalent to Step 3 below: (define Step3 (double1 lam1) My-Inc) 5); then (double1 lam1) expand: (define step4 (lambda (x) (lam1 (lam1 X) My-Inc) 5 )); place my-Inc in Lambda in Step 4: (define step5 (lam1 (lam1 my-Inc) 5 )); restore the lam1 to the original definition: (define step6 (lam1 (double3 (double3 my-Inc) 5); (double3 my-Inc) expand: (define step7 (lam1 (double3 (lambda (x) (my-Inc X) 5 )); define Lambda in Step 7 as lam2 :( define (lam2 X) (my-Inc X ))); then step 7 can be converted to: (define step8 (lam1 (double3 lam2) 5); then (double3 lam2) in Step 8) expand Step 9: (define step9 (lam1 (lambda (x) (lam2 (lam2) 5); Define the lambda function in Step 9 as lam3 :( define (lam3 X) (lam2 (lam2); then step 9 can be converted to Step 10: (define step10 (lam1 lam3) 5 )); restore the lam1 in Step 10 to the original definition: (define step11 (double3 (double3 lam3) 5); (double3 lam3) expand: (define step12 (double3 (lambda (x) (lam3 (lam3 X) 5); Name the lambda function in step 12 as lam4 :( define (lam4 X) (lam3 (lam3 X); then step12 can be expressed as step13: (define step13 (double3 lam4) 5); (double3 lam4) expand: (define step14 (lambda (x) (lam4 (lam4 X) 5 )); replacing 5 with Lambda in Step 14: (define step15 (lam4 (lam4 5 ))); restore lam4 back to the original definition: (define step16 (lam3 (lam3 (lam3 (lam3 5 ))))); restore lam3 back to the original definition: (define step17 (lam2 (lam2 (lam2 (lam2 (lam2 ))))))))); restore lam2 back to the original definition: (define step18 (my-Inc (my- INC (my-Inc )))))) ))))))))))); the result is 21: (define step19 21)


The above analysis process is complex, but it is also detailed.


If we look at the abstraction, we can use another method.

Evaluate the knowledge of the following methods:

(Double) My-Inc) 5)


The double process is used to call any method nested twice.


(Double) is to nest the double call twice, the result is to nest any method call four times.

If there is (define four-time (double), The fourtime process nested calls of any method four times.

'

Further we can see that (double) is equivalent to (double four-time ).


Equivalent to (four-time X ))


Note that the two four-time nested calls are not four or four times, but four or four times, that is, 16 times.


Exercise 1.41 solves the problem. This question can also help students understand high-level functions, especially the nesting of high-level functions.


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.