In Common Lisp (SBCL 1.0.58) why does the macro OR use a gensym, but not AND?
For example,
CL-USER> (macroexpand '(and 1 2 3 4 5))
(IF 1
(AND 2 3 4 5)
NIL)
T
CL-USER> (macroexpand '(or 1 2 3 4 5))
(LET ((#:G967 1))
(IF #:G967
#:G967
(OR 2 3 4 5)))
T
CL-USER>
I looked at defboot.lisp where the macros are defined but found nothing relevant in the comments.
That’s because the implemented logic operators are intended to be short-circuiting and to return the value produced by the last form they evaluated.
To achieve this,
anddoes not need agensymbecause the last form it evaluates will either produceNILor be the result of the final tail call to itself.On the other hand,
orhas to return the first non-NILvalue it evaluates, so it cannot rely on the tail call. It needs agensymto do that, because without one:1appears twice in the expansion, and in our case that means the expression that produces1is evaluated twice. And you never want that in your macros.