我是Haskell的新手。 我得到了newtype pair which overloads plus operator
代码:
newtype Pair a b = Pair (a,b) deriving (Eq,Show)
instance (Num a,Num b) => Num (Pair a b) where
Pair (a,b) + Pair (c,d) = Pair (a+c,b+d)
Pair (a,b) * Pair (c,d) = Pair (a*c,b*d)
Pair (a,b) - Pair (c,d) = Pair (a-c,b-d)
abs (Pair (a,b)) = Pair (abs a, abs b)
signum (Pair (a,b)) = Pair (signum a, signum b)
fromInteger i = Pair (fromInteger i, fromInteger i)
main = do print Pair (1, 3)
当我尝试使用ghc --make
编译文件时,收到以下错误消息
BigNumber.hs:11:11:
Couldn't match expected type `(t1, t2) -> t0'
with actual type `IO ()'
The function `print' is applied to two arguments,
but its type `((a0, b0) -> Pair a0 b0) -> IO ()' has only one
In a stmt of a 'do' block: print Pair (1, 3)
In the expression: do { print Pair (1, 3) }
我的目标是创建一个文件,对某种newtype
执行某些操作,然后打印出结果。
答案 0 :(得分:4)
将main
更改为
main = do print (Pair (1, 3))
或
main = do print $ Pair (1, 3)
或(main
由单个表达式组成的最佳选项)
main = print $ Pair (1, 3)
您必须向print
提供一个参数,而不是2。