Haskell 中 case 语句中的模式匹配变量 [英] Pattern matching variables in a case statement in Haskell

查看:30
本文介绍了Haskell 中 case 语句中的模式匹配变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是一个示例会话:

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?

推荐答案

有关原因,请参阅 Don 的回答.做你想做的事情的一个常见习语是这样的:

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"

当然在这种情况下我们会丢失 case 并且直接在函数上写守卫:

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

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

更新

这些天MultiWayIf 扩展以略少的语法噪音做到这一点.

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"

这篇关于Haskell 中 case 语句中的模式匹配变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆