Skip to content

Instantly share code, notes, and snippets.

@WillNess
Last active December 12, 2015 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WillNess/4747476 to your computer and use it in GitHub Desktop.
Save WillNess/4747476 to your computer and use it in GitHub Desktop.
"Generating primes in Haskell
"I have been learning Haskell over the last few days, through Learn You A Haskell.
I've been attempting to complete some Project Euler problems, some of which require
primes. However the function I have written to try to generate some (in this case
primes below 20000) isn't outputting correctly. When I run it, GHCi returns '[1, ' and
seemingly doesn't terminate. The code I am using is:
> sieve :: (Integral a) => a -> [a] -> [a]
> sieve 20000 list = list
> sieve n (x:xs) = sieve (n+1) $ x:(filter (\q -> q `mod` n /= 0) xs)
> primesto20000 = sieve 2 [1..20000]
"And then I am calling primesto20000. I understand that the function may be inefficient,
I am mainly asking for help on syntactic/process errors that I must have made.
Thankyou
haskell project-euler primes
asked Feb 5 at 8:43
bennybdbc"
----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment