2
votes

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

3
You have one excess closing bracket.Mirzhan Irkegulov
What happens with 6 6 6 as input? :-)6502

3 Answers

3
votes

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))))
1
votes

As sindikat pointed out, an excess closing bracket. Sorry about that.

1
votes

What about

(define (largest-of-two-sum-of-squares x y z)
    (+ (square x) (square y) (square z)
       (- (square (min x y z)))))

?