0

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)))
Tanveer Salim
  • 27
  • 1
  • 1
  • 9

1 Answers1

0

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.

Renzo
  • 26,848
  • 5
  • 49
  • 61
  • You are right! I realized this just before you are commented. What are your suggestions to prevent syntax errors like these? – Tanveer Salim May 12 '18 at 05:37
  • 1
    You can use an editor that highligths closed parentheses and automatically align properly the code. For instance the DrRacket IDE, or the editor Emacs. – Renzo May 12 '18 at 05:38