ars*_*vis 6 haskell pattern-matching
我遇到过一种我想在运营商上进行模式匹配的情况.但是,这会Pattern match(es) are overlapped导致GHC出错.我无法弄清楚为什么.不允许运营商进行模式匹配吗?我假设因为括号中的运算符符号将其转换为标识符,所以这应该有效.
test :: (Integer -> Integer -> Integer) -> String
test (+) = "plus"
test (-) = "minus"
test _ = "other"
还有其他方法可以完成我想要做的事情.我只是好奇为什么这不起作用.
Cac*_*tus 12
(+)并且(-)不是类型的构造函数Integer -> Integer -> Integer:
Integer -> Integer -> Integer 不是代数数据类型因此,您的代码等同于使用任何其他变量名来绑定第一个参数,例如
test foo = "plus"
test bar = "minus"
test _   = "other"
希望能够清楚地表明所有三种模式实际上都匹配任何东西(前两种模式绑定了一些名称).换句话说,第一个模式(foo或者(+)在您的示例中)无法通过,这就是为什么它与剩余的两个重叠.
| 归档时间: | 
 | 
| 查看次数: | 429 次 | 
| 最近记录: |