今天,我在 REPL 中犯了一个打字错误,并发现了一个奇怪的行为。以下是交互示例:
Welcome to Racket v6.11.
> (define x 3)
> x
3
> x'
3
> x
'x
>
因此,我输入的第一个 x
结果是 3
,这是预期的。我不小心输入的 x'
结果是 3
,这是意外的。最后一个 x
结果是 'x
(!!)。
我似乎不明白 REPL 如何读取值。有人可以解释一下为什么 REPL 会这样吗?
最佳答案
参见Racketrivia: Using '
as an "identifier suffix"在 Racket 邮件列表上。 reply by Robby Findler和 reply by Matthias Felleisen两者都解释了这一点。
第一行x
是正常的。
第二行,x'
,实际上是一个“表达式半”。它被解释为表达式x
,后跟未完成表达式'
。未完成的表达式允许在下一行完成。无论您在下一行中放置什么内容,都将作为'下一行
的后半部分放置。
这意味着第三个 x
实际上被解释为 ' x
的后半部分。
您可以看到带有括号的未完成表达式的更好示例:
> 1 (list 2 ; 1 followed by an unfinished expression
1
> 3 4) ; finishing it
'(2 3 4)
> 3 4) ; on its own without the `(list 2` before it it's an error
3
4
; readline-input:13:3: read-syntax: unexpected `)` [,bt for context]
表达式后面的引号以类似的方式解释:
> 1 ' ; 1 followed by an unfinished expression
1
> (indefatigable inexhaustible) ; finishes previously unfinished, put after a quote
'(indefatigable inexhaustible)
> (indefatigable inexhaustible) ; on its own without the quote before it it's an error
; indefatigable: undefined;
; cannot reference an identifier before its definition
; in module: top-level
; internal name: indefatigable
关于racket - 在 Racket REPL 中的标识符上放置 "prime"后出现奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52555639/