Skip to content

Instantly share code, notes, and snippets.

@yfyf
Created February 25, 2015 10:06
Show Gist options
  • Save yfyf/929089ede19ae4498352 to your computer and use it in GitHub Desktop.
Save yfyf/929089ede19ae4498352 to your computer and use it in GitHub Desktop.
Primes a la Yves Bertot
---- 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