Haskell: class and function overloading:

Viewed 138

I am quite new to Haskell. I am trying to overload a custom function 'myadd' for different containers. My understanding is that you have to do it through classes. This is what i tried:

class Addition a where
  myadd :: a -> a -> a

instance Addition Int where
  myadd a b = a + b

instance Addition Maybe where
  myadd (Just a) (Just b) = Just (a + b)

main = do
  let a = 3 :: Int
  let b = 4
  let c = myadd a b
  print c

  let d = (Just a)
  let e = (Just b)
  let f = myadd d e
  print f

But i get the following errors:

test.hs:7:19: error:
    ? Expecting one more argument to ‘Maybe’
      Expected a type, but ‘Maybe’ has kind ‘* -> *’
    ? In the first argument of ‘Addition’, namely ‘Maybe’
      In the instance declaration for ‘Addition Maybe’
2 Answers
Related