是什么原因导致“无可辩驳的模式失败的模式”?这意味着什么?

发布于 2024-11-23 16:08:33 字数 86 浏览 2 评论 0原文

什么是

无可辩驳的模式失败了

意思? 什么情况会导致这个运行时错误?

What does

irrefutable pattern failed for pattern

mean?
What cases will cause this runtime error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一萌ing 2024-11-30 16:08:33

考虑这个例子:

foo ~(Just x) = "hello"
main = putStrLn $ foo Nothing

这使用了无可辩驳的模式(~ 部分)。无可辩驳的模式总是“匹配”,所以这会打印 hello

foo ~(Just x) = x
main = putStrLn $ foo Nothing

现在,模式仍然匹配,但是当我们尝试使用 x 但它实际上并不存在时,我们得到了一个无可辩驳的模式匹配错误:

Irr.hs: /tmp/Irr.hs:2:1-17: Irrefutable pattern failed for pattern (Data.Maybe.Just x)

这与没有匹配时得到的错误有微妙的区别模式:

foo (Just x) = x
main = putStrLn $ foo Nothing

这个输出

Irr.hs: /tmp/Irr.hs:2:1-16: Non-exhaustive patterns in function foo

当然,这是一个有点人为的例子。更可能的解释是它来自 let 绑定中的模式,按照 chrisdb 的建议

Consider this example:

foo ~(Just x) = "hello"
main = putStrLn $ foo Nothing

This uses an irrefutable pattern (the ~ part). Irrefutable patterns always "match", so this prints hello.

foo ~(Just x) = x
main = putStrLn $ foo Nothing

Now, the pattern still matched, but when we tried to use x when it wasn't actually there there we got an irrefutable pattern match error:

Irr.hs: /tmp/Irr.hs:2:1-17: Irrefutable pattern failed for pattern (Data.Maybe.Just x)

This is subtly distinct from the error you get when there's no matching pattern:

foo (Just x) = x
main = putStrLn $ foo Nothing

This outputs

Irr.hs: /tmp/Irr.hs:2:1-16: Non-exhaustive patterns in function foo

Of course, this is a somewhat contrived example. The more likely explanation is that it came from a pattern in a let binding, as chrisdb suggested.

花之痕靓丽 2024-11-30 16:08:33

好吧,我认为它的意思就是它所说的——模式不匹配,但别无选择。这个例子:

但是对于程序来说:

gx = 让 y = fx in hy

GHC 报告:

主要:M1.hs:9:11-22:
    无可辩驳的模式对于模式 Data.Maybe.Just y 失败 

指出失败的根源。

来自 http://www.haskell.org/haskellwiki/Debugging

该示例的要点是如果 f x 返回 Nothing,那么 GHC 就无法为 y 赋值。

Well, I assume it means what it says - that a pattern doesn't match but there is no alternative. This example:

But for the program:

g x = let Just y = f x in h y 

GHC reports:

Main: M1.hs:9:11-22:
    Irrefutable pattern failed for pattern Data.Maybe.Just y 

Indicating the source of the failure.

Comes from http://www.haskell.org/haskellwiki/Debugging

The point of the example is that if f x returns Nothing then there is no way GHC can assign a value to y.

夜深人未静 2024-11-30 16:08:33

要添加其他人所说的内容,如果您断开的列表小于您想要的列表,那么从技术上讲您可以获取它。例如(在 GHCi 中):

Prelude> let l = [1,2,3]
Prelude> let (x:x1:xs) = l
Prelude> x
1

工作正常,但如果您这样做:

Prelude> let l2 = [1]
Prelude> let (x:x1:xs) = l2
Prelude> x
*** Exception: <interactive>:294:5-18: Irrefutable pattern failed for pattern (x : x1 : xs)

To add what others have said, you could technically get it if you're disconnecting a list that's smaller than what you're intending. For example (in GHCi):

Prelude> let l = [1,2,3]
Prelude> let (x:x1:xs) = l
Prelude> x
1

Works fine, but if you did:

Prelude> let l2 = [1]
Prelude> let (x:x1:xs) = l2
Prelude> x
*** Exception: <interactive>:294:5-18: Irrefutable pattern failed for pattern (x : x1 : xs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文