On Fri, May 18, 2007 at 09:55:37PM -0700, Rob Pike wrote: > The Haskell version is quite pretty but almost a special case due > to the way lazy lists work. The power series program also comes > out quite well in Haskell. The systems examples I talked about > don't work quite so well, I believe. Channels are not really the > same as lazy lists. Haskell's lazy lists make for one of the most beautiful implementations I've seen, and I agree that the CSP implementations is one of the most beautiful pieces of code I've seen. If we're to compare channels to any language feature, though, why not closures? I won't argue for replacing one with the other of course, but there are some interesting similarities. It's apparant to most readers that the CSP implementation is basically checking that each number is 0 modulo any of the previous primes, so applying a lambda to a list of previous primes immediately came to mind. But this first implementation is almost an exact mapping of the CSP implementation into lambdas and closures. Each new channel is replaced with a new closure around the same lambda (the first being the exception, as in CSP). I've also implemented it in Limbo, for the hell of it, and for the sake of the topic. :) As it turns out, the first implementation is the fastest. (defun get-sieve () (let* ((n 1) (fn (lambda () (incf n)))) (lambda () (let ((n (funcall fn)) (f fn)) (labels ((me () (let ((m (funcall f))) (if (= (mod m n) 0) (me) m)))) (setf fn #'me) n))))) (defun get-sieve () (let ((n 1) (list nil)) (labels ((prime (n) (if (member-if (lambda (x) (= (mod n x) 0)) list) (prime (1+ n)) n))) (lambda () (prog1 (setf n (prime (1+ n))) (setf list (cons n list))))))) (defun primes (num) (let ((sieve (get-sieve))) (dotimes (n num) (print (funcall sieve))))) (defun getprimes (num) (let ((sieve (get-sieve))) (do ((n 0 (funcall sieve))) ((> n num)) (print n)))) nofactor(aux: int, l: list of int): int { for(; l != nil; l = tl l) if(aux % hd l == 0) return 0; return 1; } primes(num: int) { l : list of int; n := 2; for(i := 0; i < num; n++) { if(nofactor(n, l)) { l = n :: l; i++; sys->print("%d\n", n); } } } -- Kris Maglione In any hierarchy, each individual rises to his own level of incompetence, and then remains there.