例Haskell - 如何简单地在实例中使“div”或“/”?
data SomeData = Number Integer | String String | Bool Bool
deriving Eq
而且,我想用 'DIV' 与SomeData。
instance Num SomeData where
(Number x) + (Number y) = Number $ x + y -- It's Ok.
(Number x) - (Number y) = Number $ x - y -- It's Ok.
(Number x) * (Number y) = Number $ x * y -- It's ok.
instance Integral SomeData Where
(Number x) `div` (Number y) = Number $ x `div` y
但我得到了错误消息。
main.hs:105:10:
No instance for (Real SomeData)
arising from the superclasses of an instance declaration
Possible fix: add an instance declaration for (Real SomeData)
In the instance declaration for `Integral SomeData'
main.hs:105:10:
No instance for (Enum SomeData)
arising from the superclasses of an instance declaration
Possible fix: add an instance declaration for (Enum SomeData)
In the instance declaration for `Integral SomeData'
好吧,如果我想使用div
与SomeData数据,应该我写Real Instance
和Enum Instance
为SomeData一步一步?还是有另一种好方法?
是不是div的问题,返回一个真正的,你没有一个类型构造函数的SomeData数字真实? – Paolo
@Paolo No.' div :: Integral a - > a - > a - > a'。 – fuz