Search code examples
schemesicp

Scheme function won't print


Please tell me why the following Scheme function won't print the result. I'm using DrRacket.

#lang sicp

(define (sqr x) (* x x))

(define (sum_of_greatest_squares a b c)
  (if(> a b)
       (if(> a c)
             (if(> b c)
                  ((+ (sqr a) (sqr c))(+ (sqr a) (sqr b))))
             (+ (sqr a) (sqr c))
        )
       (if(> b c)
            (if (> a c)
                  ((+ (sqr a) (sqr b))(+ (sqr b) (sqr c)))
            (+ (sqr c) (sqr b))))
       ))
(sum_of_greatest_squares 3 4 5)

Solution

  • Its plain to see that you haven't been very detail-oriented with your learning. Your code contains countless mistakes and you've allowed yourself to bring habits from other languages into your learning of scheme. DrRacket clearly shows there's a syntax error with your program, so that's a good place to start.

    The sections I have bolded are missing an else expression

    (define (sum_of_greatest_squares a b c)
      (if(> a b)
           (if(> a c)
                 (if(> b c)
                      ((+ (sqr a) (sqr c))(+ (sqr a) (sqr b))))
                 (+ (sqr a) (sqr c))
            )
           (if(> b c)
                (if (> a c)
                      ((+ (sqr a) (sqr b))(+ (sqr b) (sqr c)))
                (+ (sqr c) (sqr b))))
           ))
    

    The formatting of your code is bad and thus makes it harder to see where mistakes were made.

    Moving along, here's another big mistake

    ((+ (sqr a) (sqr c))(+ (sqr a) (sqr b)))
    

    You can't just put () wherever you want, like you can in some languages. () are used for procedure application in Lisp/Scheme/Racket.

    Say a = 1, b = 2, and c = 3, the above line would be the equivalent of

    ((+ (sqr 1) (sqr 3))(+ (sqr 1) (sqr 2)))
    ((+ 1       9      )(+ 1       4      ))
    (10                                   5)
    

    The last line is (10 5) which says "apply the number 5 to the number 10" – which makes no sense.

    You make the same mistake later in the code too. Try fixing up these problems first and then update your question.

    ps: don't use snake_case names like sum_of_greatest_squares. Schemers typically would format that as sum-of-greatest-squares.


    It's not immediately clear what your procedure is supposed to do anyway. My guess is that it's supposed to square the two largest inputs and then sum them.

    You could do a huge hierarchy of if expressions to decide which two arguments to perform a computation with... but another way would be to always use the same two arguments to compute the result, but use if expressions to move the appropriate arguments into the correct place.

    Consider this as a lateral alternative to your procedure

    (define (sqr x) (* x x))
    
    (define (sum-of-greatest-squares a b c)
      (cond ((< a b) (sum-of-greatest-squares b c a))
            ((< b c) (sum-of-greatest-squares a c b))
            (else (+ (sqr a) (sqr b)))))
    

    This will always sum the squares of the two greatest numbers, but does so in a much cleaner way


    Lastly, Jörg mentions that you're not actually printing anything anywhere. I know you mean to imply that DrRacket isn't showing the result of your program in the Interactions window. However, if you do want to explicitly print to the console, look at the display* and print* procedures