我目前正在阅读sicp书的练习1.3 . 这是问题的描述:
定义一个过程,该过程将三个数字作为参数,并返回两个较大数字的平方和 .
我尝试使用以下代码解决它
(define (square x) (* x x))
(define (sq2largest a b c)
((define large1 (if (> a b) a b))
(define small (if (= large1 a) b a))
(define large2 (if (> c small) c small))
(+ (square large1) (square large2))))
当我在mit-scheme中运行它时,我收到以下错误:
;无法在null语法环境中绑定名称:large1#[reserved-name-item 13]
谷歌搜索此错误不会产生很多结果 . 有谁知道我的代码有什么问题? (我不熟悉Scheme)
2 回答
你有太多的括号 . 如果你取出内部定义的额外括号,事情应该会好很多 .
我将尝试分解sq2largest过程的结构:
基本结构是:
你写的身体是:
因此,Body的结构如下:
其转换为:“将bravo,charlie和delta作为alpha的参数 . ”
现在,alpha被告知要接受一堆参数,但是在为large1保留的命名空间内,没有为任何参数做出任何规定......即,方案遇到一个无法绑定任何变量的空语法环境 .
括号在Scheme(以及大多数,如果不是全部,Lisps)中都很重要,因为它们定义了过程的范围并强制执行[1]操作的应用顺序 .
[1] "No ambiguity can arise, because the operator is always the leftmost element and the entire combination is delimited by the parentheses." http://mitpress.mit.edu/sicp/full-text/sicp/book/node6.html