I have a bit of code:
(defun divisor (x)
(loop for i from 2 to x do
(if (= x i) (return x)
(if (not (mod x i))
(return (append i (divisor (/ x i))))))))
that should return a list of the prime factors of x. However, the code just returns x.
The defun evaluates with no errors. I’ve attempted to trace every function in the defun, and none is ever evaluated. Loop is a macro, so I can’t trace it, but if I clear out the inside of the loop and replace with
(format t "~d " i)
it counts from 2 to x, like I would expect.
I assume I’ve done something wrong, but I can’t figure it out.
One problem is that you are using
(not (mod x i))to determine if themodevaluation is 0, which is wrong.Another problem is that you are appending atoms to lists and I don’t think it is exactly what you want to do.
Here is a revised version that seems to work: