I'm trying to write a sieve of Erasthosthenes function that gives a User all Primes from 2 to his upper Limit. So I've written this code:
main = do
putStrLn "Upper Limit"
g <- readLn
let sieve [] = []
let sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0]
let primes = sieve [2..g]
print primes
The Code compiles and is giving me the right solution but I'm getting this exception at the end of the solution: *** Exception: Non-exhaustive patterns in function sieve So I've checked what patterns aren't matched.
warning: [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In an equation for `sieve': Patterns not matched: (_:_)
warning: [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In an equation for `sieve': Patterns not matched: []
Wich I don't understand since I've given let sieve [] = []
And I thought _ in Haskell means any variable so what does the pattern (:) mean?
Any help would be appreciated.