3 ms·
Well it's the haskell.org landing page, of course they're showing off the type of code that Haskell is good at. In your link, the purely functional version (15
by throwaway_yy2Di 12y ago
Well it's the haskell.org landing page, of course they're showing off the type of code that Haskell is good at. In your link, the purely functional version (15 lines around a priority queue) is more complicated than a naive version with a mutable array, so why would they advertise it?
- tel 12y agoI personally think the priority queue one is somewhat simpler than a fixed array algorithm. It also produces an infinite list of primes (or generator) instead of the primes up to a particular number. This is especially important in Haskell. I'd love to see it as it shows off some great data structures available in the libraries (like priority queues) but it's getting longish. The mutable array version is basically the same as imperative code everywhere sieveUA :: Int -> UArray Int Bool sieveUA top = runSTUArray $ do let m = (top-1) `div` 2 r = floor . sqrt $ fromIntegral top + 1 sieve <- newArray (1,m) True forM_ [1..r `div` 2] $ \i -> do isPrime <- readArray sieve i when isPrime $ do forM_ [2*i*(i+1), 2*i*(i+2)+1..m] $ \j -> do writeArray sieve j False return sieve It's a bit noisy syntactically and doesn't show off as much interesting stuff.
- ufo 12y agoMy pet peeve is mainly the function name. It says the algorithm is a prime sieve when it isn't and it spreads a common misconception :)