week1 - CS 61A FIRST LAB: Week 1 Lab and Homework Solutions...

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

View Full Document Right Arrow Icon
CS 61A Week 1 Lab and Homework Solutions FIRST LAB: No problems that required original solutions! SECOND LAB: 1. Nothing original. 2. If the last letter is Y, then we have to look at the next-to-last: (define (plural wd) (if (and (equal? (last wd) 'y) (not (vowel? (last (bl wd))))) (word (bl wd) 'ies) (word wd 's))) If you didn't think to use AND in that way, it can be done with nested IFs: (define (plural wd) (if (equal? (last wd) 'y) (if (vowel? (last (bl wd))) (word wd 's) (word (bl wd) 'ies)) (word wd 's))) Or, if that's too messy, with a subprocedure: (define (plural wd) (if (equal? (last wd) 'y) (y-plural (bl wd)) (word wd 's))) (define (y-plural prefix) (if (vowel? (last prefix)) (word prefix 'ys) (word prefix 'ies))) All of these assume the definition of vowel? in the handout. 3. There are, of course, many possible ways to write this. None is perfectly elegant. The difficulty is figuring out which of the three arguments is smallest, so you can leave it out of the computation. The way I like best, I think, is a little tricky: (define (sum-square-large papa mama baby) (define (square x) (* x x)) (cond ((> mama papa) (sum-square-large mama papa baby)) ((> baby mama) (sum-square-large papa baby mama)) (else (+ (square papa) (square mama))))) I think this way is pretty concise and easy to read. However, it's okay if you did it more straightforwardly like this: (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq x y) (+ (square x) (square y)))
Background image of page 1

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

View Full DocumentRight Arrow Icon
(cond ((and (<= a b) (<= a c)) (sumsq b c)) ((and (<= b a) (<= b c)) (sumsq a c)) (else (sumsq a b)) )) If you didn't think of using AND to identify the conditions, it could also be done using nested IFs: (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq x y) (+ (square x) (square y))) (if (>= a b) (if (>= b c) (sumsq a b) (sumsq a c)) (if (>= a c) (sumsq a b) (sumsq b c)))) Some people wanted to start by solving a subproblem: a function to find the two largest numbers. This can be done, but it's harder: (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq nums) (+ (square (first nums)) (square (last nums)))) (define (two-largest a b c) (cond ((and (<= a b) (<= a c)) (sentence b c)) ((and (<= b a) (<= b c)) (sentence a c)) (else (sentence a b)))) (sumsq (two-largest a b c))) The trick here is that a function can't return two values, so two-largest has to return a sentence containing the two numbers. This hardly seems worth the effort, but the attempt to split the problem into logical pieces was well-motivated. It's a good idea in general, but it didn't work out well this time. 4.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This homework help was uploaded on 04/09/2008 for the course CS 61A taught by Professor Harvey during the Spring '08 term at Berkeley.

Page1 / 6

week1 - CS 61A FIRST LAB: Week 1 Lab and Homework Solutions...

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

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