CS 60A  Solution to homework #1 Fall 1993
15 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 (sumsquarelarge 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 (sumsquarelarge 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 (sumsquarelarge 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 (sumsquarelarge 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 (sumsquarelarge a b c)
(define (square x) (* x x))
(define (sumsq nums) (+ (square (first nums)) (square (last nums))))
(define (twolargest a b c)
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document(cond ((and (<= a b) (<= a c)) (sentence b c))
((and (<= b a) (<= b c)) (sentence a c))
(else (sentence a b))))
(sumsq (twolargest a b c)))
The trick here is that a function can't return two values, so twolargest
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 wellmotivated.
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 (sumsquarelarge papa mama baby)
(define (square x) (* x x))
(cond ((> mama papa) (sumsquarelarge mama papa baby))
((> baby papa) (sumsquarelarge baby papa mama))
((> baby mama) (sumsquarelarge papa baby mama))
(else (+ (square papa) (square mama)))))
Maybe the simplest way is to use min, a builtin scheme function .
..
(define (sumsquarelarge 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 openended problem (child, children; alumnus, alumni) but
here's a version that does some words correctly:
(define (plural wd)
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '08
 Harvey

Click to edit the document details