hw1 - CS 60A Solution to homework#1 Fall 1993 1-5 didn't...

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

View Full Document Right Arrow Icon
CS 60A -- Solution to homework #1 Fall 1993 1-5 didn't require any original thinking. 6. 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. Here is a straightforward solution: (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq x y) (+ (square x) (square y))) (cond ((and (<= a b) (<= a c)) (sumsq b c)) ((and (<= b a) (<= b c)) (sumsq a c)) (else (sumsq a b)) )) Some additional thought suggests that some of the testing is unnecessary (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq x y) (+ (square x) (square y))) (cond ((and (<= a b) (<= a c)) (sumsq b c)) ;; if we continue to here, a is not the smallest of a b c so ((<= b c) (sumsq a c)) (else (sumsq a b)) )) or even (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq x y) (+ (square x) (square y))) (if (and (<= a b) (<= a c)) (sumsq b c) (sumsq a (max b c)))) 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 want 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)
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)) (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. The following solution wins the cuteness award, but it's a bit tricky: (define (sum-square-large papa mama baby) (define (square x) (* x x)) (cond ((> mama papa) (sum-square-large mama papa baby)) ((> baby papa) (sum-square-large baby papa mama)) ((> baby mama) (sum-square-large papa baby mama)) (else (+ (square papa) (square mama))))) Maybe the simplest way is to use min, a built-in scheme function . .. (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq x y) (+ (square x) (square y))) (cond ((equal? a (min a b c)) (sumsq b c)) ((equal? b (min a b c)) (sumsq a c)) (else (sumsq a b)) )) 7. This is an open-ended problem (child, children; alumnus, alumni) but here's a version that does some words correctly: (define (plural wd)
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.

Page1 / 6

hw1 - CS 60A Solution to homework#1 Fall 1993 1-5 didn't...

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