On 5/22/07, Bakul Shah <[EMAIL PROTECTED]> wrote:
> > 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.
The channel itself has no memory of what was transferred, but I can't think of a good reason one couldn't also memoize the remainder computation needed such that if the spawned process/thread were to stay alive for multiple runs, one could avoid re-computing the result, which is what Haskell is doing, except that it's also probably caching the results of "take" applied to "primes" in your case :-)
