> > http://en.literateprograms.org/Sieve_of_Eratosthenes_%28Haskell%29

> I'm not sure that's even valid Haskell though.   Looks like two type
> definitions for the same function, and even if you correct that I
> don't think it produces what one might think it does.  At least not on
> GHC :-)

The second type definition on the referred page is not needed
(actually you don't need either defn).  The function works
but you have to give it [2..], all natural numbers >= 2.  The
prime sieve definition I like most is this:

    sieve (a:xs) = a:sieve [x | x<- xs, x `mod` a > 0]
    primes = sieve [2..]

Now you can just do

        100 `take` primes

to get first 100 primes.

A stream such as primes will (and must) return the same Nth
value every time so after computing a value it will be cached
for later.  A stream in effect has storage proportional to
the number of cdr (i.e. tail) operations done on it.  A
channel on the other hand needs fixed minimum storage to
affect one value transfer and it has no memory of what was
already transferred.

Reply via email to