Here is a macro which I wrote to test a number for primality:
(defmacro primep (num)
`(not (or ,@(loop for p in *primes* collecting `(= (mod ,num ,p) 0)))))
*primes*
is a dynamic variable which contains a list of the primes generated so far (the context is a 'next prime generator' function). Here are the results of a few eval-ed statements:
(let ((*primes* (list 2 3)))
(primep 6))
-> T
(let ((*primes* (list 2 3)))
(macroexpand-1 '(primep 6))
-> (NOT (OR (= (MOD 6 2) 0) (= (MOD 6 3) 0)))
-> T
(NOT (OR (= (MOD 6 2) 0) (= (MOD 6 3) 0)))
-> NIL
What is going on?
LET
binding for*PRIMES*
happens at run-time, not during macroexpansion. – jkiiski(= (mod ...
statements using a function? I thought that was what macros are for (I've just started learning Common Lisp). – shardulc says Reinstate Monica