2

I'm having trouble figuring out how to sort out the 2 biggest numbers and return them into the sum of squares procedure. I am struggling to write the code out in Scheme's syntax. I'm trying to write it as cleanly as possible, and I keep running circles in my head and on paper trying to do so. the book describes thinking "procedurally" and I think I'm having trouble with that aspect.

The book provides code for the sum-of-squares and square procedures. I would include my pseudo code but I'm severely lost. Here is the code the book provides:

(define (square x) (* x x))

(define (sum-of-squares x y)
  (+ (square x) (square y)))

How to define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers?

GAD3R
  • 4,317
  • 1
  • 23
  • 34
Chance
  • 49
  • 6
  • 3
    And your question is ...? – rsm Jan 13 '19 at 07:51
  • Possible duplicate of [SICP Exercise 1.3 request for comments](https://stackoverflow.com/questions/161666/sicp-exercise-1-3-request-for-comments) – GAD3R Jan 16 '19 at 17:37

4 Answers4

3

How to define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers?

First you need a name for the procedure. Let's called it sum-of-squares-two-largest.

(define (sum-of-squares-two-largest x y z)
   ...)

It can make use of the sum-of-squares function, but it needs to find the two largest numbers out of x,y,z first.

One way to do this would be to get rid of the smallest number. You could define a helper procedure smallest? a b c that checks that a is the smallest of the 3 numbers by doing (and (<= a b) (<= a c)).

(define (sum-of-squares-two-largest x y z)
   (if (smallest? x y z)
       (sum-of-squares y z)
       (if (smallest? y x z)
           ...]
river
  • 1,028
  • 6
  • 16
1

Write the code for min-of-three. Its negative (as in photography) is what you need:

(define (negative-min-of-three a b c)
   (if (<= a b)
       (if (<= a c)
           (..... b ... c .....)
           (..... a ... b .....))
       (if (<= 
   ..........

You can complete the code, and rename it. The clock is ticking!

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • in case a downvoter was put off by the last sentence in this answer, it was a reference to OP's profile's statement, intended as an encouragement. I have no idea otherwise what could have been the reason. – Will Ness Jan 14 '19 at 18:56
1

I created two methods to get the largest and the one in the middle for the time being.

(define (largest x y z)
  (cond ((and (> x y) (> x z)) x)
        ((and (> y x) (> y z)) y)
        (else z))
)

(define (mid x y z)
  (cond ((and (> x y) (< x z)) x)
        ((and (> y x) (< y z)) y)
        (else z))
)

(define (square a)
  (* a a)
)

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

(sum-of-two-largest -12 -4 1)
user1044328
  • 85
  • 1
  • 7
1

The hard part of this is, if you're reading SICP book, finding the 2nd largest number of the three. You can observe, if we let c < b < a, that:

a = max(a, b) gives the largest of the two numbers
b = max(c, b) gives the largest of the two smaller numbers

But how do we get the variable b on the second line. It can so happen that a is the smaller of the two? We can observe that:

b = min(a, b)

If we substitute min(a, b) for b in the max function on the third line, we get:

b = max(c, min(a, b))

This strategy is implemented in the following code, using only the constructs introduced in the book so far:

(define (square x) (* x x))

(define (max a b) (if (> a b) a b))

(define (min a b) (if (< a b) a b))

(define (sum-of-squares-two-largest a b c)
   (+ (square (max a b)) (square (max c (min a b)))))

(sum-of-squares-two-largest 1 2 3)
(sum-of-squares-two-largest 1 2 1)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Gary Houbre Nov 19 '21 at 08:25