Exercise 1.3 SICP Error: Cannot evaluate expression in Racket

Exercise 1.3 SICP Error: Cannot evaluate expression in Racket

我正在做 SICP 的练习 1.3。

我的代码如下:

#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)

令人惊讶的是,Racket 解释器确实如此

不打印上述任何结果。口译员

适用于其他值。但是对于

这组三个值不是

正在工作。

当我尝试添加 else 语句时

喜欢以下内容:

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

口译员说:

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

你在函数 sum-of-biggest-squares 中有几个语法错误:应该添加一个括号来关闭第一个 cond 子句,并且 else 应该添加到第二个子句:

(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)))))

请注意,您格式化代码的方式与当前的通用约定如此不同,因此很难阅读并且容易引入语法错误。