{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# hw5 - CS 61A HOMEWORK 2.24 Week 5 solutions(list 1(list...

This preview shows pages 1–4. Sign up to view the full content.

CS 61A Week 5 solutions HOMEWORK: 2.24. (list 1 (list 2 (list 3 4))) The printed result is (1 (2 (3 4))). The box and pointer diagram (in which XX represents a pair, and X/ represents a pair whose cdr is the empty list): --->XX--->X/ | | | | V V 1 XX--->X/ | | | | V V 2 XX--->X/ | | | | V V 3 4 [NOTE: The use of XX to represent pairs, as above, is a less-readable form of box-and-pointer diagram, leaving out the boxes, because there's no "box" character in the ASCII character set. This is okay for diagrams done on a computer, but when you are asked to *draw* a diagram, on a midterm exam for example, you should use actual boxes, as in the text and the reader.] The tree diagram: + / \ / \ 1 + / \ / \ 2 + / \ / \ 3 4 2.26. Finger exercises. Given (define x (list 1 2 3)) (define y (list 4 5 6)) > (append x y) (1 2 3 4 5 6) > (cons x y)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
((1 2 3) 4 5 6) ;; Equivalent to ((1 2 3) . (4 5 6)) but that's not how ;; it prints! > (list x y) ((1 2 3) (4 5 6)) 2.29 Mobiles. Many people find this exercise very difficult. As you'll see, the solutions are quite small and elegant when you approach the problem properly. The key is to believe in data abstraction; in this problem some procedures take a MOBILE as an argument, while others take a BRANCH as an argument. Even though both mobiles and branches are represented "below the line" as two-element lists, you won't get confused if you use the selectors consistently instead of trying to have one procedure that works for both data types. (a) Selectors. They give us the constructor (define (make-mobile left right) (list left right)) The corresponding selectors have to extract the left and right components from the constructed list: (define (left-branch mobile) (car mobile)) (define (right-branch mobile) (cadr mobile)) Note that the second element of a list is its CADR, not its CDR! Similarly, the other selectors are (define (branch-length branch) (car branch)) (define (branch-structure branch) (cadr branch)) (b) Total weight: The total weight is the sum of the weights of the two branches. The weight of a branch may be given explicitly, as a number, or may be the total-weight of a smaller mobile. (define (total-weight mobile) (+ (branch-weight (left-branch mobile)) (branch-weight (right-branch mobile)) )) (define (branch-weight branch) (let ((struct (branch-structure branch))) (if (number? struct) struct (total-weight struct) ))) The LET isn't entirely necessary, of course; we could just say (branch-structure branch) three times inside the IF.
(c) Predicate for balance. It looks like we're going to need a function to compute the torque of a branch: (define (torque branch) (* (branch-length branch) (branch-weight branch) )) Here we have used the BRANCH-WEIGHT procedure from part (b) above. Now, they say a mobile is balanced if two conditions are met: The torques of its branches must be equal, and its submobiles must be balanced. (If a branch contains a weight, rather than a submobile, we don't have to check if it's balanced. This is the base case of the recursion.)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 13

hw5 - CS 61A HOMEWORK 2.24 Week 5 solutions(list 1(list...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online