Questions tagged [non-exhaustive-patterns]
73 questions
31
votes
2 answers
In Haskell, why non-exhaustive patterns are not compile-time errors?
This is a follow-up of Why am I getting "Non-exhaustive patterns in function..." when I invoke my Haskell substring function?
I understand that using -Wall, GHC can warn against non-exhaustive patterns. I'm wondering what's the reason behind not…
gawi
- 13,050
- 7
- 38
- 74
29
votes
2 answers
Non-exhaustive patterns in function
I've got a problem with this code, it should count the longest substring of the same letter in a string, but there is an error:
*** Exception: test.hs:(15,0)-(21,17):
Non-exhaustive patterns in function countLongest'
I know that is the wrong types…
kaz
- 1,873
- 1
- 13
- 19
12
votes
2 answers
How to systematically avoid unsafe pattern matching in Scala?
Consider the following broken function:
def sum (list : Seq[Int]) : Int = list match {
case Nil => 0
case head :: tail => head + sum(tail)
}
Here, the function was supposed to work with a List[Int], but was refactored to accept Seq[Int]…
Rotsor
- 13,009
- 5
- 40
- 57
11
votes
2 answers
Haskell: non-exhaustive-patterns
I am training for a test tomorrow to complete my introduction to functional programming but there is one thing I don't understand.
Whenever I have a program like:
test [] = []
test (x:xs) = test (xs)
What he does is that he takes the first element…
Hans van der Laan
- 515
- 3
- 16
10
votes
1 answer
Why am I getting "Non-exhaustive patterns in function..." when I invoke my Haskell substring function?
I'm working my way through the book The Haskell Road to Logic, Maths and Programming. (I'm only mid-way through chapter 1, but I'm enjoying it so far and intend to continue.) I've read through the section 1.5 "Playing the Haskell Game" which…
Daryl Spitzer
- 121,723
- 75
- 151
- 166
8
votes
1 answer
GHC complains about non-exhaustive patterns that are enforced by the type checker
I have the following code
{-# LANGUAGE DataKinds, GADTs, TypeOperators #-}
data Vect v a where
Nil :: Vect '[] a
Vec :: a -> Vect v a -> Vect (() ': v) a
instance Eq a => Eq (Vect v a) where
(==) Nil Nil = True
(Vec…
user2407038
- 13,827
- 2
- 25
- 39
6
votes
2 answers
Match expression on Int is not exhaustive
I've started learning Scala.
I was surprised that next code compiles:
object Hello extends App {
def isOne(num: Int) = num match {
case 1 => "hello"
}
}
You can't do something similar in Rust for example.
Why Scala compiler does not force…
captain-yossarian
- 6,577
- 1
- 8
- 21
6
votes
2 answers
Surjectivity check when return type is sealed
Scala can warn when pattern match on a sealed type is not exhaustive, however can we check that a function returns all cases when the return type is sealed? For example, consider the following ADT
sealed trait Foo
case object Bar extends Foo
case…
Mario Galic
- 41,266
- 6
- 39
- 76
5
votes
1 answer
What does "(_:_:_)" mean in Haskell (non-exhaustive pattern matching error from GHCI)?
I'm getting the following error from GHCI when I run my Haskell program:
"Pattern match(es) are non-exhaustive
In an equation for `recaList': Patterns not matched: (_:_:_)"
I've been searching the web/SO, but can't seem to find an explanation for…
Caroline
- 105
- 7
5
votes
3 answers
Why doesn't this definition cover all pattern cases?
So I'm trying to triplize an element, i.e. making 2 other copies of the element.
So I've written this:
triplize :: [a] -> [a]
triplize [x] = concatMap (replicate 3) [x]
But I've been getting this error:
Non-exhaustive patterns in function…
user32132321
- 127
- 1
- 1
- 9
3
votes
2 answers
How do I relax the non-exhaustive patterns check for a nested match on known variants?
How do I persuade the Rust compiler that the internal match expression is fine here, as the outer match has already restricted the possible types?
enum Op {
LoadX,
LoadY,
Add,
}
fn test(o: Op) {
match o {
Op::LoadX |…
dying_sphynx
- 986
- 4
- 15
3
votes
2 answers
Avoid inappropriate non-exhaustive pattern match warning in GHCI
Before you dismiss this as a duplicate
I see that at least as of September 2018, GHCI does not allow you to disable a warning locally (although you can in a whole file).
But maybe there's some other way to let GHCI know that every case is in fact…
Jeffrey Benjamin Brown
- 2,618
- 2
- 21
- 33
3
votes
2 answers
Pattern match(es) are non-exhaustive
I'm trying to create a function that eliminates multiples of a given Integer from a list of Integers, in the form multiples x [y], where x is the given Integer, and y is the list.
Here's what I have:
multiples :: Integer -> [Integer] ->…
Erakura
- 35
- 1
- 5
3
votes
2 answers
Pattern Match Fail when `function [] _ = ...; function _ [] = ...` syntax is omitted
Though disjoint exhausts all possible patterns in its guard conditions, Haskell gives me a PatternMatchFail error when running it.
disjoint :: (Ord a) => [a] -> [a] -> Bool
disjoint l@(x:xs) r@(y:ys)
| null l || null r = True
| x == y …
Nicholas Montaño
- 915
- 10
- 22
3
votes
1 answer
Haskell: Fix non-exuastive patterns in function
I have a program that traverses an AST and returns a map of the functions and variables used and how many times they occurred. Here it is:
import Data.Map
import Language.Haskell.Exts.Syntax
increment :: Ord a => a -> Map a Int -> Map a…
user2548080
- 197
- 7