我使用 chicken Scheme 解释器,当我输入 not
时, 它返回 #<procedure (not x295)>
, 但当我尝试 and
和 or
, 它返回 Error: unbound variable: and
.
似乎 and
运算符不是内置过程,为什么它不同于 not
?
如果这是真的,那么(and #t #f)
又如何呢?评价?
最佳答案
and
和 or
是“短路运算符”——它们不会计算超过形成结果所必需的值,就像 if
和 cond
。例如:
(and #f (display "foo"))
不会显示任何东西,这也不会:
(or #t (display "foo"))
使用过程不可能做到这一点,因为对过程的调用将在进入过程之前评估所有参数:
(define (proc-or x y) (if x #t (if y #t #f)))
(proc-or #t (display "foo"))
; displays "foo" anyway
if
(或者,实际上,or
)这样的特殊形式可以决定它希望评估哪些参数。请参阅任何有关条件运算符或更一般的特殊形式(例如 MIT Scheme、Racket ...)的 Scheme 手册
更具体地说,or
和 and
要么是原始形式(如 if
),要么是将运算符转换为一系列的宏原始形式。
关于scheme - 为什么方案中的 `and` 运算符不是内置过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26902224/