变量与环境

介绍如何用 define 定义变量、变量绑定,以及存储绑定关系的环境概念。

定义变量

我们用 define 给变量(variable)赋值。

例如,(define x 2) 把值 2 赋给 x:

-> (define x 2)
-> x
2
-> (+ x 5)
7

调用 (define x 2) 之后,我们说 x 被绑定(bound)到 2。变量绑定存储在环境(environment)中,我们将在 Unit 3 中更详细地讨论环境。

检查理解

假设在 Racket 解释器中输入以下内容:

(define pi 3.14159)
(define radius 3)

(define area (* radius radius pi))
(define circumference (* 2 pi radius))

area 会求值为什么?circumference 会求值为什么?