First, there's no need to use globals here, at all.
Use true/false return values. That would allow your is-prime
function to be something like:
(defun is-prime (n)
(cond ((= 2 n) t) ;; Hard-code "2 is a prime"
((= 3 n) t) ;; Hard-code "3 is a prime"
((evenp n) nil) ;; If we're looking at an even now, it's not a prime
(t ;; If it is divisible by an odd number below its square root, it's not prime
(loop for i from 3 to (isqrt n) by 2
never (zerop (mod n i))))))
That way, the function is not relying on any external state and there's nothing that can confuse anything.
Second, the last 1
you see is (probably) the return value from the function.
To check that, try: (progn (prime-numbers 10) nil)
Third, re-write your prime-numbers
function to not use global variables.
Fourth, never create global variables with setf
or setq
, use either defvar
or defparameter
. It's also (mostly, but some disagree) good style to use *earmuffs*
on your global (really, "special") variables.