2

I am new to Haskell. I got newtype pair which overloads plus operator

The code:

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)

When I try to compile the file with ghc --make I get the following error message

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) }

My goal is to create a file that does something to a some sort of newtype then prints out the result.

Community
  • 1
  • 1
Sahar Rabinoviz
  • 1,939
  • 2
  • 17
  • 28
  • 1
    Note to SO users: this is __not__ a typo problem - it's a precedence issue. – AndrewC May 08 '14 at 15:09
  • 1
    @AndrewC Full ACK. I think this is one of the most annoying pitfalls for Haskell beginners, because you can't see what you did wrong. Voting to leave open. – Uli Köhler Jun 25 '14 at 16:28

1 Answers1

4

Change you main into

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

or

main = do print $ Pair (1, 3)

or (the best option since your main consists of a single expression)

main = print $ Pair (1, 3)

You have to give a single argument to print, not 2.

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109