Haskell打印对

时间:2014-05-05 23:36:52

标签: haskell

我是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执行某些操作,然后打印出结果。

1 个答案:

答案 0 :(得分:4)

main更改为

main = do print (Pair (1, 3))

main = do print $ Pair (1, 3)

或(main由单个表达式组成的最佳选项)

main = print $ Pair (1, 3)

您必须向print提供一个参数,而不是2。