I was reading on different sieving algorithms when I stumbled upon a kind of improved version of the Sieve of Eratosthenes called Euler’s Sieve. According to Wikipedia there is an implementation of an slightly different version of the idea (called Turner’s Sieve) in Haskell.
Now I am trying to understand what exactly the code snippet given does and I think I’ve got it but now I wanted to translate the code into F# and have really no idea where to start. My main concern is that there doesn’t seem to be a function to “substract” two sequences.
Here’s the code:
import Data.OrdList (minus)
primes = euler [2..]
euler (p : xs) = p : euler (xs `minus` map (*p) (p : xs))
How would this be implemented in F#? Is it even possible?
If you want to calculate things like merges/differences of infinite lists like Haskell does, the LazyList type (found inside the F# power pack) springs to mind.
It makes for very verbose code, like the translation below:
with
Note that I added two
failwithclauses to keep the compiler from complaining about an incomplete match, even if we know that all lists in the calculation are (lazily) infinite.