I'm writing a function that takes an input a list, creates sublists, and retrieves n elements that it outputs into a new list. I'm writing guards depending on the value of the inputs, but I keep getting the error "Patterns not matched: (:) _".
Anyone identifies the issue?
nKsets :: [Int] -> Int -> [[Int]]
nKsets [] _ = error "Empty list should not be given as input"
nKsets l n
| n <= 0 = []
| n > 0 = ...
The error I'm getting is:
Pattern match(es) are non-exhaustive
In an equation for ‘nKsets’: Patterns not matched: (_:_) _
CodePudding user response:
The exhaustivity matcher doesn't know that all values of n
are either greater than 0 or not greater than 0, so it assumes that your second definition is not exhaustive. Use otherwise
instead of n > 0
to guarantee that a guard succeeds.
nKsets :: [Int] -> Int -> [[Int]]
nKsets [] _ = error "Empty list should not be given as input"
nKsets l n
| n <= 0 = []
| otherwise = ...
You can also use a third definition instead of two guards on the second:
nKsets :: [Int] -> Int -> [[Int]]
nKsets [] _ = error "Empty list should not be given as input"
nKsets l n | n <= 0 = []
nKsets l n = ...