hw6 - CS 61A Week 6 solutions HOMEWORK: 2.24. (list 1 (list...

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

View Full Document Right Arrow Icon
Sheet1 Page 1 CS 61AWeek 6 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Sheet1 Page 2 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) ((1 2 3) 4 5 6) > (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 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
Background image of page 2
Sheet1 Page 3 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 (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.) (define (balanced? mobile) (and (= (torque (left-branch mobile))
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/14/2009 for the course PEIS 100 taught by Professor Mckenzie during the Spring '08 term at Berkeley.

Page1 / 42

hw6 - CS 61A Week 6 solutions HOMEWORK: 2.24. (list 1 (list...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online