Search code examples
schemesicp

what is wrong this scheme code?


I'm studying sicp. this question is ex 1.3. I can't understand why this code is problem. please help me.. TT

here's the code.

(define (test a b c)
        (cond ((and (< a b) (< a c)) (+ (* b b) (* c c))
               (and (< b a) (< b c)) (+ (* a a) (* c c))
               (else (+ (* b b) (* c c)))
        ))

(test 1 2 3)

error is

Premature EOF on #[input-port 60 from buffer at #[mark 61 #[buffer 17] 166 left]


Solution

  • Your syntax for cond is wrong. Here is the same code with the correct syntax:

    (define (test a b c)
      (cond ((and (< a b) (< a c)) (+ (* b b) (* c c)))
            ((and (< b a) (< b c)) (+ (* a a) (* c c)))
            (else (+ (* b b) (* c c)))))
    

    However, your code is still wrong. Can you see why? (Hint: what does the else branch signify, and what expression should be there?)