non-exhaustive-patterns

Haskell: Non-exhaustive patterns in function (simple functions) [duplicate]

天涯浪子 提交于 2021-02-05 09:37:39
问题 This question already has answers here : Better exception for non-exhaustive patterns in case (2 answers) Closed 4 years ago . I am confused as to why the 1st and 3rd versions of this functions give this error whereas the second definition works fine. -- head and tail third :: [a] -> a third [a] = head (tail (tail[a])) -- Pattern matching third2 :: [a] -> a third2 (_:_:x:_) = x -- List indexing third3 :: [a] -> a third3 [a] = [a]!!2 Thanks in advance 回答1: That is odd that the second one does

Surjectivity check when return type is sealed

自闭症网瘾萝莉.ら 提交于 2020-01-01 09:50:40
问题 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 object Qux extends Foo Then function f: Foo => String on the algebraic data type Foo def f(x: Foo): String = x match { case Bar => "bar" } raises warning match may not be exhaustive. It would fail on the following input: Qux def f(x: Foo) = x match {

In Haskell, why non-exhaustive patterns are not compile-time errors?

时间秒杀一切 提交于 2019-12-28 16:30:13
问题 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 making it a compile-time error by default given that it's always possible to explicitly define a partial function: f :: [a] -> [b] -> Int f [] _ = error "undefined for empty array" f _ [] = error "undefined for empty array" f (_:xs) (_:ys) = length

Haskell - Non-exhaustive patterns in case

做~自己de王妃 提交于 2019-12-25 02:17:03
问题 I have got the following code: F (S core ps) = FAll core [] ps where FAll core acc ((name, (pc : pcs)) : ps) = case F' (pc : pcs) (readC pc core) core of Nothing -> if (length pcs) /= 0 then FAll core ((name, pcs) : acc) ps else FAll core acc ps Just (core', [pc']) -> let pc'' = pc' `mod` coresize pcs' = pcs ++ [pc''] in FAll core' ((name, pcs') : acc) ps stepAll core acc [] = S core (reverse acc) It compiles fine but when I run the program it gives the following error: Melon.hs:(172,10)-(182

Why am I getting “Non-exhaustive patterns in function…” when I invoke my Haskell substring function?

孤人 提交于 2019-12-23 07:26:48
问题 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 "consists of a number of further examples to get you acquainted with [Haskell]". So far I've learned about functions, type declarations, guarded equations, a little about list pattern matching, and where & let. I'm stuck on exercise 1.17, which asks us to

GHC complains about non-exhaustive patterns that are enforced by the type checker

可紊 提交于 2019-12-19 16:36:16
问题 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 e0 v0) == (Vec e1 v1) = e0 == e1 && v0 == v1 When compiling or interpreting with -Wall the following warning is given: Pattern match(es) are non-exhaustive In an equation for `==': Patterns not matched: Nil (Vec _ _) (Vec _ _) Nil Normally this is to be expected. Normally, even if

GHC complains about non-exhaustive patterns that are enforced by the type checker

时间秒杀一切 提交于 2019-12-19 16:36:10
问题 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 e0 v0) == (Vec e1 v1) = e0 == e1 && v0 == v1 When compiling or interpreting with -Wall the following warning is given: Pattern match(es) are non-exhaustive In an equation for `==': Patterns not matched: Nil (Vec _ _) (Vec _ _) Nil Normally this is to be expected. Normally, even if

Haskell: Non-exhaustive pattern - Checking if list is ascending

回眸只為那壹抹淺笑 提交于 2019-12-13 01:27:26
问题 I have no idea why my function doesn't work. I have gone through all the posts about non-exhaustive functions but my functions fulfills all possible options as far as I can see. ascending :: [Int] -> Bool ascending [] = error "Empty list given" ascending [x] = True ascending [x,y] | y>=x = True | x<y = False ascending (x:y:xs) | y>=x = (ascending (y:xs)) | x<y = False Result: *Main> ascending [] *** Exception: Empty list given *Main> ascending [1] True *Main> ascending [1, 2] True *Main>

Surjectivity check when return type is sealed

妖精的绣舞 提交于 2019-12-04 05:16:08
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 object Qux extends Foo Then function f: Foo => String on the algebraic data type Foo def f(x: Foo): String = x match { case Bar => "bar" } raises warning match may not be exhaustive. It would fail on the following input: Qux def f(x: Foo) = x match { Is it possible to raise a similar non-exhaustion warning when return type is an ADT such as in the

Haskell: non-exhaustive-patterns

╄→гoц情女王★ 提交于 2019-12-03 17:39:13
问题 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 out of the list and continues with the rest. Whenever there is just one left, xs should be [] which in turn should trigger test [] = [] . But whenever I run this algorithm I get an error. Exception: <interactive>:20:5-16: Non-exhaustive patterns in