为什么我的代码中出现此错误: 代码:
module Task5 where
import Prelude
data Stream a = a :& Stream a
infixl 4 :&
add :: Num a => a -> a -> a
add a b = a + b
instance Num (Stream a ) where
(+) (ia:&a) (ib :& b) = (ia + ib) :& ((+) a b)
错误:
Task5.hs:14:33:
No instance for (Num a) arising from a use of `+'
Possible fix:
add (Num a) to the context of the instance declaration
In the first argument of `(:&)', namely `(ia + ib)'
In the expression: (ia + ib) :& ((+) a b)
In an equation for `+':
+ (ia :& a) (ib :& b) = (ia + ib) :& ((+) a b)
我不明白如何纠正这个问题,我整整一周都试过了,但找不到任何解决办法。所以,你能告诉我我应该纠正什么吗?
答案 0 :(得分:4)
如错误所示,您需要将Num a
添加到实例声明的“上下文”中:
instance Num a => Num (Stream a) where
否则ia + ib
操作没有任何(+)
操作可用,因为它对各个流成员起作用。