Primes by Trial Division - INTERLISP
23 September 2016
The only change between this and the LISP/80 version (which I wrote first) is to take into account differences in the way functions are meant to be defined.
Variant A
(DEFINEQ (PRIMES (LAMBDA (N) (PROG (COUNT PRIMES) (SETQ COUNT 3) (SETQ PRIMES (LIST 2)) NEXT (COND ((LESSP (LENGTH PRIMES) N) (COND ((MEMBER 0 (MAPCAR PRIMES '(LAMBDA (X) (REMAINDER COUNT X)))) NIL) (T (NCONC PRIMES (LIST COUNT))))) (T (RETURN PRIMES))) (SETQ COUNT (PLUS COUNT 1)) (GO NEXT)))))
Variant B
(DEFINEQ (DIVP (LAMBDA (N LIST) (PROG NIL (MAPC LIST '(LAMBDA (P) (COND ((ZEROP (REMAINDER N P)) (RETURN T))))) (RETURN NIL)))) (PRIMES (LAMBDA (N) (PROG (COUNT PRIMES) (SETQ COUNT 3) (SETQ PRIMES (LIST 2)) NEXT (COND ((LESSP (LENGTH PRIMES) N) (COND ((DIVP COUNT PRIMES) NIL) (T (NCONC PRIMES (LIST COUNT))))) (T (RETURN PRIMES))) (SETQ COUNT (PLUS COUNT 1)) (GO NEXT)))))