为什么 Racket 使用定义 window 和 REPL 对 REPL 和突变进行不同的处理?

Why Racket treats differently mutation on REPL and mutation using both the definition window and the REPL?

通常,我避免使用突变,因为你很少需要它们。

但是,我需要它们,并且我正在尝试了解更好的东西。有一个特定的行为让我很感兴趣,我想请求你的帮助以更好地理解它。

如果我在 REPL 上输入,下面的更改一切正常:

> (define x 1)
> (set! x (+ x 1))
> x
2

如果我把赋值和变异放在定义上 window 它也有效:

(define y 1)
y
(set! y (+ y 1))
y

在 运行 文件之后,我可以在 REPL 上看到以下正确结果:

1
2
>

但是,如果我把定义变量x放在定义windows上,如果我尝试设置!它在 REPL 上的新值我得到一个错误:

; Definition Window

(define y 1)

;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;

; REPL

> (set! y (+ y 1))
. . y: cannot modify a constant

为什么会这样? 交互式编程不应该特别用于这种情况吗?

提前致谢。

Matthias Felleisen了解,目前负责Racket开发的主要负责人之一:

The DrRacket REPL allows set!s on those variables that are assignable in the module and no others. That's by design. Roughly speaking, think of the Definitions window as a module and the REPL a way to perform computations as if we were a client of the module that can also use all of its internally defined functions, think super-client. But this super-client view does not enable you to mutate variables that weren't mutable in the first place .. just like a client can't do that either.

此信息是在 Racket's Mail list 7 年前提出的,正如我在上面 post 中对自己的问题的评论中所指出的那样。