Haskell 中 case 语句中的模式匹配变量

发布于 2024-09-14 09:56:37 字数 820 浏览 5 评论 0原文

如果我使用 case 语句将字符串文字与字符串文字进行比较,我会得到预期的行为:如果它们相同 - 则匹配,如果不同 - 则不匹配。

但是,如果我将字符串文字与字符串常量进行比较,则会收到“模式匹配重叠”警告,并且具有常量的分支始终匹配。

这是一个示例会话:

Prelude> let var1 = "abc"
Prelude> let var2 = "def"
Prelude> case var1 of { var2 -> "Fail"; _ -> "Win" }

<interactive>:1:0:
    Warning: Pattern match(es) are overlapped
             In a case alternative: _ -> ...
"Fail"
Prelude> case "abc" of { var2 -> "Fail"; _ -> "Win" }

<interactive>:1:0:
    Warning: Pattern match(es) are overlapped
             In a case alternative: _ -> ...
"Fail"
Prelude> case "abc" of { "def" -> "Fail"; _ -> "Win" }
"Win"

同时,如果行为符合预期:

> Prelude> if var1 == var2 then "Fail" else "Win" 
"Win"

这里发生了什么?这种行为有何意义?

If I compare a string literal to a string literal using the case statement, I get the expected behavior: if they are the same - it matches, if they are not - it does not.

However, if I compare a string literal to a constant that is a string, I get "Pattern matches are overlapped" warning and the branch with the constant always matches.

Here's an example session:

Prelude> let var1 = "abc"
Prelude> let var2 = "def"
Prelude> case var1 of { var2 -> "Fail"; _ -> "Win" }

<interactive>:1:0:
    Warning: Pattern match(es) are overlapped
             In a case alternative: _ -> ...
"Fail"
Prelude> case "abc" of { var2 -> "Fail"; _ -> "Win" }

<interactive>:1:0:
    Warning: Pattern match(es) are overlapped
             In a case alternative: _ -> ...
"Fail"
Prelude> case "abc" of { "def" -> "Fail"; _ -> "Win" }
"Win"

Meanwhile, if behaves as expected:

> Prelude> if var1 == var2 then "Fail" else "Win" 
"Win"

What's going on here? How does this behavior make sense?

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

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

发布评论

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

评论(3

山色无中 2024-09-21 09:56:37

请参阅 Don 的回答了解原因。执行您尝试执行的操作的常见习惯用法是:

var1 = "abc"
var2 = "def"

foo x = case () of
    () | x == var1 -> "Fail"
       | x == var2 -> "Failzor"
       | otherwise -> "WIN"

当然,在这种情况下,我们会丢失 case 并直接在函数上编写防护:

foo x | x == var1 = "Fail"
      | ...

UPDATE

这些MultiWayIf 扩展以稍微较少的语法噪音来实现此目的。

{-# LANGUAGE MultiWayIf #-}

foo x = if | x == var1 -> "Fail"
           | x == var2 -> "Failzor"
           | otherwise -> "WIN"

See Don's answer for why. A common idiom for doing what you are trying to do is this:

var1 = "abc"
var2 = "def"

foo x = case () of
    () | x == var1 -> "Fail"
       | x == var2 -> "Failzor"
       | otherwise -> "WIN"

Of course in this case we would lose the case and just write the guards directly on the function:

foo x | x == var1 = "Fail"
      | ...

UPDATE

These days the MultiWayIf extension does this with slightly less syntactic noise.

{-# LANGUAGE MultiWayIf #-}

foo x = if | x == var1 -> "Fail"
           | x == var2 -> "Failzor"
           | otherwise -> "WIN"
樱花落人离去 2024-09-21 09:56:37

Haskell 中的模式匹配 绑定新变量。因此,当您编写:

case x of
    y -> ...

您现在已将新变量“y”绑定到“x”的值。这就是平凡的“模式”。您可以更清楚地看到当涉及构造函数时绑定是如何工作的:

case x of 
    (a, b) -> ...

现在 a 和 b 绑定到元组的组件。等等用于解构和绑定其他数据类型。
因此,要匹配字符串文字,您可以编写:

case x of
    "def" -> ....

Pattern matching in Haskell binds new variables. So when you write:

case x of
    y -> ...

you have now bound a new variable 'y' to the value of 'x'. This is the trivial "pattern". You can see more clearly how the binding works when a constructor is involved:

case x of 
    (a, b) -> ...

Now a and b bind to components of the tuple. And so on for deconstructing and binding other data types.
Thus, to match a string literal, you would write:

case x of
    "def" -> ....
戈亓 2024-09-21 09:56:37

那是因为“案例”并没有按照你的想法进行。设置为“def”的“var2”不与“var1”进行比较。相反,您将获得一个新作用域,其中包含绑定到“var1”值的新“var2”。

出现错误消息的原因是,就编译器而言,“var2 ->...”和“_ -> ...”之间没有区别。两者都匹配“var1”的所有可能值。

That's because the "case" isn't doing what you think it is. The "var2" that was set to "def" is not being compared with "var1". Instead you are getting a new scope containing a new "var2" that is bound to the value of "var1".

The reason for the error message is that as far as the compiler is concerned there is no difference between "var2 ->..." and "_ -> ...". Both match all possible values of "var1".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文