[Haskell-cafe] Looking for the fastest Haskell primes algorithm

ajb at spamcop.net ajb at spamcop.net
Thu Apr 16 22:16:01 EDT 2009

G'day all.

Quoting Eugene Kirpichov <ekirpichov at gmail.com>:

> I'd suggest also
> primesFrom :: Integer -> [Integer]


primes :: [Integer]

isn't as useful as you might think for a library, because it must, by
definition, leak an uncontrolled amount of memory.  This:

primesUpTo :: Integer -> [Integer]

is a better interface in that respect.

For the number theory library, I went overboard with a bunch of type
classes.  I don't have the code handy, but this was the sort of thing:

class TestableProperty s a | s -> a where
     is :: s -> a -> Bool

data Prime = Prime

class TestableProperty Prime Integer where
     is Prime n = {- ... -}

Then you could add instances for Fibonacci or whatever you wanted.

Andrew Bromage

More information about the Haskell-Cafe mailing list