Created
February 25, 2015 10:06
-
-
Save yfyf/929089ede19ae4498352 to your computer and use it in GitHub Desktop.
Primes a la Yves Bertot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---- Prime sieve based on co-inductive streams | |
---- Taken from "Filters on coinductive streams, an application to | |
---- Eratosthenes’ sieve" by Yves Bertot | |
-- a 'stateful' filter | |
fm p n (x:xs) | (n < x) = fm p (n+p) (x:xs) | |
fm p n (x:xs) | (n == x) = fm p (n+p) xs | |
fm p n (x:xs) | (x < n) = x : (fm p n xs) | |
primes = sieve [2..] where | |
sieve (p:xs) = p : (sieve $ fm p (2*p) xs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment