"SICP Exercise" 59 Exercise 2.29

Source: Internet
Author: User



Practice 2.29

This problem, and the previous Rectangle topic, the name of variables, functions, and so on is to make people crazy. Let's start by writing the Left-branch and right-branch in the title.

(Define (Left-branch mobile)

(Car mobile))

(Define (Right-branch mobile)

(Cadr mobile))

Note that this is Cadr , not cdr. The corresponding branch-length and branch-structure.

(Define (Branch-length branch)

(Car branch))

(Define (Branch-structure branch)

(Cadr branch))

a The small question is not difficult,B the total-weight in the question is also asks each part Branch-structure. And according to the meaning of the topic, if one branch is hanging another activity body, then the weight of the moving body is the weight of this branch, otherwise the structure of the branch is the weight of the branch. And judging if this branch has other active bodies, can we use pair? To judge. The total weight is the sum of the weights of the left and right parts.

(Define (Branch-weight branch)

(if (pair? (Branch-structure Branch))

(total-weight (branch-weight branch))

(branch-structure Branch)))

(Define (Total-weight mobile)

(+ (Branch-weight (Left-branch Mobile))

(Branch-weight (Right-branch Mobile))))

Let's test the results.

(Define First-mobile (Make-mobile (make-branch 20 10)

(MAKE-BRANCH20 40)))

(Total-weight Mobile)

; Value:50

(Define Second-mobile (Make-mobile (Make-branch-First-mobile)

(make-branch 100 180)))

; valeu:230

The following starts to do the C quiz. Let's look at the moment - the length of the left pole multiplied by the weight of the rod, equal to the same product on the right side of the moving body. This is the first condition in which the activity body is called the balance, and the second condition is that the sub-active body that each branch hangs on is also balanced. So we can define the following process, we will first define the moment good.

(Define (Branch-force branch)

(* (Branch-length Branch)

(Branch-weight Branch)))

(Define (Mobile-balance mobile)

(Let (left (Left-branch mobile))

(Right (Right-branch mobile)))

(and (= (Branch-force left)

(Branch-force right))

(branch-balance left)

(branch-balance right))))

(Define (branch-balance. Branch)

(if (pair? (Branch-structure Branch))

(mobile-balance?) (Branch-structure Branch))

#t))

C we have finished the small problem, let's victory finish the last small question. d Small problem changed the original list to cons. So at the beginning of the right-branch and branch-structure to use cadr instead of Cdr, but here with Cdr That's right.

(Define (Left-branch mobile)

(Car mobile))

(Define (Right-branch mobile)

(Cdr mobile))

(define (Branch-length branch)

(Car branch))

(Define (Branch-structure branch)

(CDR branch))

Does it feel magical? Then let's test it.

(Define Third-mobile (Make-mobile (make-branch 20 25)

(make-branch30 40)))

; Value:third-mobile

Third-mobile

; Value: ((20.25) 30.40)

Note that the data in chapter two is abstract, so the previous mobile-balance? It can be used here as well.

"SICP Exercise" 59 Exercise 2.29

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.