问题 1
嗨,如果在 WinGHCi 中我故意执行以下错误的代码:
3 4
然后我得到的错误信息是
<interactive>:1:1:
No instance for (Num (a0 -> t0))
arising from the literal `3'
Possible fix: add an instance declaration for (Num (a0 -> t0))
In the expression: 3
In the expression: 3 4
In an equation for `it': it = 3 4
究竟是什么No instance for (Num (a0 -> t0))
意思?
问题2
为什么下面的代码:
(+) 2 3 4
<interactive>:1:7:
No instance for (Num (a0 -> t0))
arising from the literal `3'
Possible fix: add an instance declaration for (Num (a0 -> t0))
In the second argument of `(+)', namely `3'
In the expression: (+) 2 3 4
In an equation for `it': it = (+) 2 3 4
产生与第二段代码略有不同的错误:
2+3 4
<interactive>:1:3:
No instance for (Num (a1 -> a0))
arising from the literal `3'
Possible fix: add an instance declaration for (Num (a1 -> a0))
In the expression: 3
In the second argument of `(+)', namely `3 4'
In the expression: 2 + 3 4
即在第一段代码中,我们No instance for (Num (a0 -> t0))
在第二段代码中拥有 where No instance for (Num (a1 -> a0))
。
[对 ehird 的回应]
(从答案评论中移出的问题):
1)我很欣赏后两种表达方式的不同,但你是说我不应该试图理解为什么解释器选择(Num (a0 -> t0))
前者和(Num(a1 -> a0))
后者,除了它们不同的事实吗?
2)嗨,当你说“但没有函数的 Num 实例”时,前者是什么意思?抱歉,我不清楚实例的概念是什么。此外,出于好奇,您能否使用您的实例Num (a -> b)
方法以某种方式告诉解释器解释3 4
为4 modulo 3
?