haskellexceptionghci# Non-exhaustive patterns in function defined in GHCi

I'm trying to write a sieve of Eratosthenes function that gives a user all Primes from 2 to their 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: []
```

Which 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.

Solution

The problem is that you **define sieve in two separate let statements**. As a result the Haskell compiler thinks that you define

`sieve`

functions`sieve`

lacks a `(_:_)`

pattern and the latter the `[]`

pattern.If you later use `sieve`

, the Haskell compiler will link to the closest one, so the latter (as a result a call to `sieve`

) in `let primes = sieve [2..g]`

will only know of the *second* `sieve`

definition (and will thus error on the end of the list).

You can solve this by using the following `let`

statement:

```
let { sieve [] = [] ; sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0] }
```

- Comparing lists in Haskell
- Is there a non-identity monad morphism M ~> M that is monadically natural in M?
- Problem with loading module ‘Distribution.Simple’
- Improving efficiency in Stirling numbers calculation
- Does sequencing an infinite list of IO actions by definition result in a never-ending action? Or is there a way to bail out?
- How to call pgQuery from postgresql-query?
- How to avoid whitespace after a tag (link) in Hamlet templates?
- Understanding type-directed resolution in Haskell with existential types
- Why is seq bad?
- Understanding bind function in Haskell
- How to create route that will trigger on any path in Servant?
- How do I use a global state in WAI middleware?
- nixos 23.11 cabal install mysql-simple problem - "Missing (or bad) C libraries"
- Is there a way to kill all forked threads in a GHCi session without restarting it?
- Why can an invalid list expression such as 2:1 be assigned to a variable, but not printed?
- Iterate over a type level list and call a function based on each type in the list
- How does this solution of Project Euler Problem 27 in the Haskell Wiki work?
- Why `Monad` is required to use `pure`?
- Can't do partial function definitions in GHCi
- recommended way to convert Double -> Float in Haskell
- Haskell profiling understanding cost centre summary for anonymous lambda
- Why is Haskell fully declarative?
- GHC Generating Redundant Core Operations
- Question about Event firing in reflex-frp
- Using Haskell's "Maybe", type declarations
- How can I elegantly invert a Map's keys and values?
- Why there is no output for wrapped IO in Haskell?
- What are the definitions of Weather and Memory in xmobar repo?
- Serializing a Data.Text value to a ByteString without unnecessary \NUL bytes
- Using Haskell with VS Code