Reputation: 963
The prompt is to define a procedure that returns the sum of the squares of the two largest of three numbers.
I know this isn't exactly an elegant solution, but this is what I hacked together:
(define (largest-of-two-sum-of-squares x y z)
(cond ((and (< x y) (< x z)) (sum-of-squares y z))
((and (< y z) (< y x)) (sum-of-squares x z))
((and (< z x) (< z y)) (sum-of-squares x y)))))
What I'm wondering is why I'm getting an error.
;The object 85 is not applicable
The number that follows the word object is always the correct answer, btw. I am a scheme beginner, it must be something in my syntax?
Thanks
Upvotes: 2
Views: 260
Reputation: 114579
What about
(define (largest-of-two-sum-of-squares x y z)
(+ (square x) (square y) (square z)
(- (square (min x y z)))))
?
Upvotes: 1
Reputation: 236140
Here's another possible solution, this one works even in the cases where all three numbers are equal or if two are equal and lower than the other:
(define (sum-max a b c)
(define (sum x y)
(+ (* x x) (* y y)))
(if (>= a b)
(if (>= b c)
(sum a b)
(sum a c))
(if (>= a c)
(sum b a)
(sum b c))))
Upvotes: 3