SICP 1.3解释器错误未知标识符:和



我正在使用浏览器中的解释器(没有任何本地设置(:https://inst.eecs.berkeley.edu/~cs61a/fa14/assets/pinterpreter/scheme.html
并获得以下解释器异常消息:

SchemeError: unknown identifier: and
Current Eval Stack:
-------------------------
0: and
1: (cond (and (< x y) (< x z)) (sqrt-sum y z))
2: (f 1 2 3)

对于以下代码:

; define a procedure that takes three numbers
; as arguments and returns the sum of the squares
; of the two larger numbers
(define (square) (* x x))
(define (sqrt-sum x y) 
(+ (square x) (square y)))
(define (f x y z)
(cond (and (< x y) (< x z)) (sqrt-sum y z))
(cond (and (< y x) (< y z)) (sqrt-sum x z))
(cond (and (< z y) (< z x)) (sqrt-sum x y)))
(f 1 2 3)

我很难找到任何关于这个解释器所基于的特定Scheme版本的信息;抱歉

这不是cond的正确语法。语法为

(cond (condition1 value1...)
(condition2 value2...)
...)

在代码中,第一个条件应该是表达式(and (< x y) (< x z))。但是条件和值周围没有括号。条件应该是and,而不是(and (< x y) (< x z))。由于and不是一个有值的变量,因此会出现错误。

正确的语法是:

(define (f x y z)
(cond ((and (< x y) (< x z)) (sqrt-sum y z))
((and (< y x) (< y z)) (sqrt-sum x z))
((and (< z y) (< z x)) (sqrt-sum x y))))

最新更新