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
然後打印出結果。
注到這樣用戶:這是__not__一個錯字的問題 - 這是一個優先問題。 – AndrewC
@ArewrewC完全確認。我認爲這是Haskell初學者最煩人的陷阱之一,因爲你看不到你做錯了什麼。投票開放。 –