Skip to main content.
-->
March 13th, 2005

Solution to SICP Exercise 1.3

Structure and Interpretation of Computer Programs

Solution to Exercise 1.3:

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

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

(define (sum-of-squares-of-two-larger x y z)
  (cond ((and (<= x y) (<= x z)) (sum-of-squares y z))
        ((and (<= y x) (<= y z)) (sum-of-squares x z))
        (else (sum-of-squares x y))))

Posted by Ken Dyck in Programming

This entry was posted on Sunday, March 13th, 2005 at 12:02 pm and is filed under Programming. You can follow any responses to this entry through the comments RSS 2.0 feed. You can leave a response, or trackback from your own site.

24 Responses to “Solution to SICP Exercise 1.3”

  1. Daniel Prager says:

    Alternatively:

    
    (define (sum-of-squares-of-two-larger a b c)
    (define (lt a b) (if (> a b) b a))
    (define (least a b c) (lt a (lt b c)))
    (define (square x) (* x x))
      (+ (square a)
          (square b)
          (square c)
          (- (square (least a b c)))))

  2. russ says:

    Another, though bad, alternative:

    (define (test x y z)
      (cond ((> x y) (if (> y z)
                         (+(* x x) (* y y))
                         (+(* x x) (* z z)))
             (> x z) (+(* y y) (* x x))
             (else (+(* z z) (* y y)))

    —-Thanks for posting your answers to SICP. I’m new to programming and am looking for other, likely better, solutions for this book.

  3. Jon Strait says:

    Another alternative:

    
    (define (sum-of-squares-two-largest a b c)
      (define (square x) (* x x))
      (define (sum-of-squares x y) (+ (square x) (square y)))
      (define (greater x y)
        (if (&gt; x y) x y))
      (if (&gt; a b)
          (sum-of-squares a (greater b c))
          (sum-of-squares b (greater a c))))
    

  4. newbie says:

    my silly newbie alternative:

    (define (square x) (* x x))
    (define (trio a b c) (cond (
    (and (

  5. newbie says:

    ups… whats wrong with my post ?? it get truncated..

  6. Ken Dyck says:

    Yeah, newbie, it looks that way. Would you mind emailing me your code? I’ll update your comment with your entire solution.

    kd@kendyck.com

  7. b forest says:

    ken when you input the same number into your code it might affect the result. for example when i put this into your code:

    (sum-of-squares-of-two-larger 2 2 3)

    it respondes “9″.

    By the way really great page. I’m so glad to see that so many people out there are working on SICP as well. It’s a really great book.

  8. b forest says:

    All so here is my code. For some reason i really feel like this is a round about way of getting things done but it seems to work so…


    (define (sum-of-square-of-2-largest-of-3 a b c)
    (define (square x)
    (* x x))
    (define (sum-of-squares t y)
    (+ (square t) (square y)))
    (define (all-equal?)
    (if (and (equal? a b) (equal? a c))
    #t
    #f))
    (define (any-equal?)
    (if (or (equal? a b) (equal? b c) (equal? a c))
    #t
    #f))
    (define (largest)
    (cond ((and (>= a b) (>= a c)) a)
    ((and (>= b c) (>= b a)) b)
    (else c)))
    (define (equal)
    (cond ((equal? a b) a)
    ((equal? b c) b)
    (else c)))
    (define (2nd-largest)
    (cond ((or (and (> a b) ( a c))) a)
    ((or (and (> b a) ( b c))) b)
    (else c)))
    (cond ((all-equal?) (sum-of-squares a a))
    ((any-equal?) (sum-of-squares (largest) (equal)))
    (else (sum-of-squares (largest) (2nd-largest)))))

  9. Ken Dyck says:

    Well isn’t that embarassing? Thanks for pointing that out, b. I’ll make a fix at my next available opportunity.

  10. Rob G says:

    Great Idea to post some of these Ken. Hope there’s no flack from profs using the book. I too have decided once again to learn this since I’ve read and been told soooooo many times it makes one a much better programmer…which is my job too.

    Any way here is my example using the regular sum-of-squares everone else has:

    (define (biggest x y) (if (> x y) x y))
    (define (smallest x y) (if (

  11. Rob G says:

    Oops seems maybe I need to escape the less than character for this to post without truncating(?).. anyway the body of the procedure is:

    (define (sumSq2Largest a b c)
      (sum-of-squares (biggest a b) (biggest (smallest a b) c)))

  12. Ken Dyck says:

    Rob, I hope you don’t mind that I formatted your code… but it seems as though it got cut off along the way.

    I presume your implementation of smallest is :

    (define (smallest x y) (if (< x y) x y))
    

    I really like your solution, btw. Very elegant.

  13. SICP по-русски » Blog Archive » Решение упражнения 1.3 из SICP says:

    [...] Еще один очень элегантный вариант решения я обнаружил в комментариях к блогу Кена Дика: [...]

  14. fire says:

    (define (maxm a b)
    (cond ((> a b) a) (else b)))
    (define (sq x) (* x x))

    (define (proc a b c)
    (if (and (> c a)
    (> c b))
    (+ (sq c)
    (sq (maxm a b)))
    (if (and (

  15. Rohini says:

    This is one more solution to the sum of squares problem:-
    (define (sq x)
    (* x x))

    (define (sq-lrg a b c)
    (cond ((and ( a c))(= a b c)) (+ (sq a)))
    (else(sq-lrg b c a))
    ))

  16. Rohini says:

    Here is the solution the previous one was a little messed up .(forgot the tags !!)

    
    (define (sq x)
    (* x x))
    
    (define (sq-lrg a b c)
    (cond ((and (<a> a c)) (= a b c)) (+(sq a)(sq a)))
          (else (sq-lrg b c a))
          ))
    

  17. Angus77 says:

    This is my solution

    (define (sum-squares-of-largest-2 x y z)
    (cond ((and (

  18. david says:

    here is my solution:

    ;procedure square
    ;given a number, returns the square
    (define square
    (lambda (x)
    (* x x)
    ))

    ;procedure biggest-2-out-of-3
    ;given 3 numbers, returns an unordered vector of the two
    ;largest
    (define biggest-2-out-of-3
    (lambda (x y z)
    (cond
    ((>= x y z) (vector x y))
    ((>= x z y) (vector x z))
    ((>= z y x) (vector z y))
    )))

    ;procedure sum-squares
    ;given 2 numbers, returns the sum of their squares
    (define sum-squares
    (lambda (x y)
    (+ (square x) (square y))
    ))

    ;procedure sum-squares-of-3
    ;given 3 numbers, returns the sum of the squares of the
    ;two largest
    (define sum-squares-of-3
    (lambda (x y z)
    (let*
    (
    (top-2 (biggest-2-out-of-3 x y z))
    (first (vector-ref top-2 0))
    (second (vector-ref top-2 1))
    )
    (sum-squares first second)
    )))

    ;main
    (display(sum-squares-of-3 2 3 3))

  19. david says:

    Argh, the formatting got all messed up. How do you post so that whitespace is preserved?

  20. david says:

    here is my solution:

    
    ;procedure square
    ;given a number, returns the square
    (define square
     (lambda (x)
      (* x x)
    ))
    
    ;procedure biggest-2-out-of-3
    ;given 3 numbers, returns an unordered vector of the two
    ;largest
    (define biggest-2-out-of-3
     (lambda (x y z)
      (cond
       ((&gt;= x y z) (vector x y)) 
       ((&gt;= x z y) (vector x z)) 
       ((&gt;= z y x) (vector z y)) 
    )))
    
    ;procedure sum-squares
    ;given 2 numbers, returns the sum of their squares
    (define sum-squares
     (lambda (x y)
      (+ (square x) (square y))
    ))
    
    ;procedure sum-squares-of-3
    ;given 3 numbers, returns the sum of the squares of the
    ;two largest
    (define sum-squares-of-3
     (lambda (x y z)
      (let* 
       (
        (top-2  (biggest-2-out-of-3 x y z))
        (first  (vector-ref top-2 0))
        (second (vector-ref top-2 1))
       )
        (sum-squares first second)
    )))
    
    ;main
    (display(sum-squares-of-3 2 3 3))
    

  21. antoszka says:

    
    (define (exercise a b c)
      (define (^2 x) (* x x))
      (if (&gt; a b)
          (+ (^2 (if (&gt; b c) b c)) (^2 a))
          (+ (^2 (if (&gt; a c) a c)) (^2 b)))
    

    This only uses what has been shown up to the moment in the book.

  22. Attila says:

    Just using what I learned of lisp from this book so far. And keeping it short as possible:

    (define (max_square a b c)
    (cond
    ((>= a b c) (+ (* a a) (* b b)))
    ((>= a c b) (+ (* a a) (* c c)))
    (else (+ (* b b) (* c c)))
    ))

  23. Stephen Brown says:

    Here’s my solution:

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

    (define (f x y z)
    (- (+ (square x)(square y)(square z)) (square (min x y z))))

  24. Roman says:

    Well, I think that perhaps it’s not pretty good to use approaches that was not explained in the book before this exercise was suggested.
    Here is my solution:

    
    (define (sum-max2-squares x y z)
        (cond ((>= x y) (+ (* x x) (if (>= y z) (* y y) (* z z))))
        (else (+ (* y y) (if (>= x z) (* x x) (* z z))))))
    

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>