模式匹配是多余的 [英] Pattern match is redundant

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

问题描述

Haskell,堆栈构建工具.

Haskell, Stack build tool.

我有代码:

quote :: Char
quote = '\''

doubleQuote :: Char
doubleQuote = '\"'

isBorder :: Char -> Bool
isBorder quote = True
isBorder doubleQuote = True
isBorder _ = False

它将被编译而不会出错,但是我在编译过程中看到了消息:

It will be compiled without erors, but I see the messages during the compilation:

D:\ haskell \ real \ app \ Main.hs:34:1:警告:[-Woverlapping-patterns]
模式匹配是多余的
在"isBorder"的等式中:isBorder doubleQuote = ...

D:\haskell\real\app\Main.hs:34:1: warning: [-Woverlapping-patterns]
Pattern match is redundant
In an equation for `isBorder': isBorder doubleQuote = ...

D:\ haskell \ real \ app \ Main.hs:35:1:警告:[-Woverlapping-patterns]
模式匹配是多余的
在"isBorder"的等式中:isBorder _ = ...

D:\haskell\real\app\Main.hs:35:1: warning: [-Woverlapping-patterns]
Pattern match is redundant
In an equation for `isBorder': isBorder _ = ...

是什么意思?我看不到冗余...

What does it mean? I don't see redundance...

推荐答案

声明函数时,参数变量是 new 名称.您的quote和doubleQuote隐藏了函数,而不是调用用于模式匹配的函数.这样,模式匹配的语言就与右侧表达的语言有所不同.要实现您想要的目标,要么做

When declaring functions, the argument variables are new names. Your quote and doubleQuote are shadowing the functions rather than invoking the functions for pattern matching. In this way the language of pattern matching subtly deviates from the language of right-hand-sided expressions. To achieve what you want, either do

isBorder :: Char -> Bool
isBorder '\'' = True
isBorder '\"' = True
isBorder _ = False

isBorder :: Char -> Bool
isBorder c | quote == c = True
isBorder c | doubleQuote == c = True
isBorder _ = False

哲学是首先将参数绑定到名称,然后然后使用防护来调用计算结果为布尔值的表达式.

The philosophy is to first bind the argument to name and then to use a guard to invoke an expression that evaluates to a boolean.

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

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