【发布时间】:2013-11-10 18:08:59
【问题描述】:
我正在尝试理解 haskell 错误消息,因为它们会让新手程序员感到困惑。我能找到的最简单的例子是这样的:
Prelude> 1 + True
<interactive>:2:3:
No instance for (Num Bool)
arising from a use of `+'
Possible fix: add an instance declaration for (Num Bool)
In the expression: 1 + True
In an equation for `it': it = 1 + True
为什么编译器不考虑参数顺序而寻找 (Num Bool)?为什么在我定义以下内容后它会起作用?
instance Num Bool where (+) a b = True;
[...]
Prelude> 1 + True
True
只有当第二个参数也是 (Num Bool) 时,我才能确保 (+) 可以应用于 (Num Bool) ?
【问题讨论】:
标签: haskell types pattern-matching typeclass