我学习哈斯克尔比较新.
我有以下抽象数据类型
data Scalar =
Scalar Integer
deriving (Eq, Show)
Run Code Online (Sandbox Code Playgroud)
我希望能够在Scaler类型上执行以下操作:
> (Scalar 10) + 1
> Scalar 11
Run Code Online (Sandbox Code Playgroud)
为此,我试着像这样制作Scalar一个num类的实例:
instance Num Scalar where
(Scalar i1) + i2 = (Scalar (i1+i2))
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我究竟做错了什么?什么是正确的方法来做到这一点?
编辑: 我得到的错误是:
Run Code Online (Sandbox Code Playgroud)Couldn't match expected type `Integer' with actual type `Scalar ' In the second argument of `(+)', namely `i2' In the first argument of `Scalar ', namely `(i1 + i2)'
haskell ×1