2014-05-05 49 views
2

我是Haskell的新手。 我NEWTYPE pair which overloads plus operatorHaskell print對

代碼:

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

注到這樣用戶:這是__not__一個錯字的問題 - 這是一個優先問題。 – AndrewC

+1

@ArewrewC完全確認。我認爲這是Haskell初學者最煩人的陷阱之一,因爲你看不到你做錯了什麼。投票開放。 –

回答

4

更改您main

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

main = do print $ Pair (1, 3) 

或(最好的選擇,因爲你main由一個單一的表達)

main = print $ Pair (1, 3) 

你必須給一個參數爲print,而不是2.

+0

thx它的工作,我會選擇這個答案,一旦我可以 –