Scheme中`or`和`and`是否短路?

In Scheme, does `or` and `and` short circuit?

Scheme中andor是否短路?

以下是lat?(原子列表)的两个实现。一个使用 condelse,另一个使用 orand。我想知道它们是否等价,答案取决于 orand 在 Scheme 中是否有短路评估。

(define lat?
  (lambda (l)
    (cond
         ((null? l) #t)
         ((atom? (car l)) (lat? (cdr l)))
         (else #f))))

(define lat?
  (lambda (l)
    (or (null? l)
        (and (atom? (car l))
             (lat? (cdr l))))))

我认为 or 短路。为什么?我知道 (car ())(cdr ()) 各自产生 Error: Attempt to apply…。如果 or 没有短路,那么 (lat? ()) 最终会计算 (car ()) 并产生错误。但是,(lat? ()) 不会产生错误,因此(通过 Modus Tollens)or 短路。这个对吗? and会不会短路?

是的,根据 r6rs specification (didn't find html version of r7rs online but here a link to the pdf version of r7rs specification,它们都短路了,请参阅第 4.2 节):

If there are no <test>s, #t is returned. Otherwise, the expressions are evaluated from left to right until a <test> returns #f or the last <test> is reached. In the former case, the and expression returns #f without evaluating the remaining expressions. In the latter case, the last expression is evaluated and its values are returned.

and & or 随后根据 test.

定义