Do and
and or
short circuit in Scheme?
The following are two implementation of lat?
(list of atoms). One uses cond
… else
and the other uses or
and and
. I was wondering if they are equivalent and the answer to that hinges on whether or
and and
have short circuit evaluation in Scheme.
(define lat?
(lambda (l)
(cond
((null? l) #t)
((atom? (car l)) (lat? (cdr l)))
(else #f))))
- uses
cond
andelse
(define lat?
(lambda (l)
(or (null? l)
(and (atom? (car l))
(lat? (cdr l))))))
- uses
or
andand
I think or
short-circuits. Why? I know (car ())
and (cdr ())
each produce Error: Attempt to apply…
. If or
didn’t short-circuit, then (lat? ())
would eventually evaluate (car ())
and produce the error. However, (lat? ())
does not produce the error, therefore (via Modus Tollens) or
short-circuits. Is this correct? And does and
short-circuit?