I am making a Common Lisp function to print the first N prime numbers. So far I've managed to write this code:

```
;globals
(setf isprime 1) ;if 1 then its a prime, 0 if not.
(setf from 1) ;start from 1
(setf count 0) ;should act as counter to check if we have already
; N primes printed
;function so far.
(defun prime-numbers (to)
(if (> count to) nil(progn
(is-prime from from)
(if (= isprime 1) (print from)(setf count (+ count 1)))
(setf isprime 1)
(setf from (+ from 1))
(prime-numbers to)))
(if (>= count to)(setf count 0) (setf from 1)))
;code to check if a number is prime
(defun is-prime(num val)
(if (< num 3) nil
(progn
(if (= (mod val (- num 1)) 0) (setf isprime 0))
(is-prime (- num 1) val))))
```

My problem is, it does not print N primes correctly. If I call `>(prime-numbers 10)`

, results are: `1 2 3 5 7 11 13 17 19 1`

, i.e. it printed only 9 primes correctly.

but then if i call `>(prime-numbers 2)`

the results are: `1 2 3 5 7 1`

what am I doing wrong here?? this is my first time to code in LISP.

UPDATE:

```
(defparameter from 1)
(defparameter count 0)
(defun prime-numbers (to)
(if (> count to)nil
(progn
(when (is-prime from)
(print from)
(setf count (+ count 1)))
(setf from (+ from 1))
(prime-numbers to)))
(when (>= count to)
(setf count 0)
(setf from 1)))
(defun is-prime (n)
(cond ((= 2 n) t)
((= 3 n) t)
((evenp n) nil)
(t
(loop for i from 3 to (isqrt n) by 2
never (zerop (mod n i))))))
```

works fine. but outputs a NIL at the end.

## comments:

`always`

in your`loop`

instead of`return-from`

.`return-from`

was within trivial reach.`never`

.