LISP

LISP is one of those languages that you either love or hate.

Ever notice that when someone says "X is something that you either love or hate." what they really mean is "Everybody hates X except for a few kooks who want to ram it down everbody's throat."

Yeah, that's LISP.

Back in the early 80's when everybody was excited about functional languages the LISP people decided that LISP was a functional language (it isn't) and that it was the best functional language. As work in functional languages progressed it became more and more LISPified until there wasn't much room left for the non-kooks. That's not to say that functional languages were a good idea. They probably weren't, but in some ways we never really got to find out.

So, here's my LISP implementation of SieveE. I may be a kook, but I'm not a LISP kook, so I hate it.

This program runs under GNU CLisp. To execute it, you must run it from the command line like this:

clisp SieveE.lsp


; create an array for the sieve


(setf candidates (make-array '(1000)))


; Set all array elements to true. Indices represent the integers


(dotimes (i 999)


(setf (aref candidates i) t)


)


; zero and one are not prime, turn them off


(setf (aref candidates 0) nil)


(setf (aref candidates 1) nil)


; 500 is good enough, 2*501 is > 1000


(dotimes (i 500)


; Find first ON element -- must be prime


(when (aref candidates i)


; Turn off all multiples of p, starting with 2*p


(dotimes (j (/ 1000 i))


(when (<(* i (+ j 2)) 1000)


(setf (aref candidates (* i (+ j 2))) nil)


)


)


)


)


; print all primes


(dotimes (i 999)


(when (aref candidates i)


(format t "~S is prime~%" i)


)


)

Click Here for the actual code.