9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Kris Maglione <bsdaemon@comcast.net>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] Concurrency and message passing with Newsqueak
Date: Sat, 19 May 2007 02:38:34 -0400	[thread overview]
Message-ID: <20070519063834.GC96831@kris.home> (raw)
In-Reply-To: <7359f0490705182155o2d137273s561ad7f983bf258a@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2750 bytes --]

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.

[-- Attachment #2: Type: application/pgp-signature, Size: 194 bytes --]

  parent reply	other threads:[~2007-05-19  6:38 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-05-18 23:45 Uriel
2007-05-19  3:24 ` Micah Stetson
2007-05-19  4:03   ` Rob Pike
2007-05-19  4:12     ` erik quanstrom
2007-05-19  4:36     ` W B Hacker
2007-05-19  4:55       ` Rob Pike
2007-05-19  5:13         ` W B Hacker
2007-05-19  6:38         ` Kris Maglione [this message]
2007-05-19 16:12           ` Russ Cox
2007-05-22 18:32       ` David Leimbach
2007-05-22 22:07         ` W B Hacker
2007-05-22 23:34         ` Bakul Shah
2007-05-23 13:32           ` David Leimbach
2007-06-01  4:59             ` Jeff Sickel
2007-06-01 11:28               ` David Leimbach
2007-06-01 13:48                 ` Russ Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070519063834.GC96831@kris.home \
    --to=bsdaemon@comcast.net \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).