Personal tools

User:WillNess

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
 
I am a newbie, interested in Haskell.
 
I am a newbie, interested in Haskell.
   
I like ''this'':
+
I like ''[http://ideone.com/qpnqe this]'':
   
 
<haskell>
 
<haskell>
primes = 2 : 3 : ([5,7..] `minus`
+
primes = 2 : g (fix g) -- double staged production idea due to M. O'Neill
foldi (\x:xs -> (x:) . union xs)
+
fix g = xs where xs = g xs
[[p*p,p*p+2*p..] | p <- tail primes])
+
g xs = 3 : (gaps 5 $ foldi (\x:xs -> (x:) . union xs)
  +
[[p*p, p*p+2*p..] | p <- xs])
  +
gaps k s@(x:xs) -- | k<=x = minus [k,k+2..] xs
  +
-- inlined to avoid a space leak
  +
= if k < x
  +
then k : gaps (k+2) s
  +
else gaps (k+2) xs
 
</haskell>
 
</haskell>
   

Revision as of 18:06, 3 August 2011

I am a newbie, interested in Haskell.

I like this:

primes = 2 : g (fix g) -- double staged production idea due to M. O'Neill
fix g = xs where xs = g xs
g xs = 3 : (gaps 5 $ foldi (\x:xs -> (x:) . union xs)
                           [[p*p, p*p+2*p..] | p <- xs])
gaps k s@(x:xs) -- | k<=x = minus [k,k+2..] xs
                -- inlined to avoid a space leak
     = if k < x 
         then k : gaps (k+2) s 
         else     gaps (k+2) xs

foldi is on Tree-like folds. More at Prime numbers.