Search code examples
racketsicpmit-scheme

Exercise 1.3 SICP Error: Cannot evaluate expression in Racket


I am doing Exercise 1.3 from SICP.

My code is the following:

#lang racket

(require sicp)

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

(define (sum-of-squares a b)
 (+ (square a) (square b)   )
                              )

(define (max a b)
(cond ((>= a b) a)
      (else b)
               )
                   )

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

(cond ((>= a b)
  (sum-of-squares a (max b c) )

  (sum-of-squares b (max a c) ) 

                        )
                        )
                            )

(sum-of-biggest-squares 5 7 10)

Surprisingly, the Racket interpreter does

not print any result for the above. The interpreter

works fine for other values. But for

this set of three values its not

working.

When I try to add an else statement

like the following:

  (else (sum-of-squares b (max a c) ) ) 

The interpreter says:

   exercise_1-3.rkt:23:10: else: not allowed as an expression
   in: (else (sum-of-squares b (max a c)))

Solution

  • You have a couple of syntax errors in function sum-of-biggest-squares: a parenthesis should be added to close the first cond clause, and else should be added to the second one:

    (define (sum-of-biggest-squares a b c)
      (cond ((>= a b) (sum-of-squares a (max b c)))
            (else (sum-of-squares b (max a c)))))
    

    Note that the way in which you format the code, so different from the current common conventions, makes very difficult to read it and easy to introduce syntax errors.