[Solved] Sum of two numbers with function in haskell


I think you may be confusing type variables which are used in type annotations, and the actual values being passed to the function.

In your definition of mygcd:

mygcd c = a + b

Unless a and b are defined elsewhere, this will be an error. In other words, these a, b, and c are different than the a, b, and c mentioned in the type annotation for the function. The ones in the type annotation refer only to types of variables.

If it is confusing, you can leave off the type annotation and let the compiler determine it for you.

For instance, if you want a function to add two numbers, it’s as simple as this:

addTwoNumbers a b = a + b

If you load this into GHCi, and check the type with :t addTwoNumbers, you’ll see that the inferred type is addTwoNumbers :: Num a => a -> a -> a.

If you would prefer the function add the contents of a tuple, you can change the definition to

addTuple (a, b) = a + b

Now the inferred type will be addTuple :: Num a => (a, a) -> a

mygcd :: Num a => (a, a) -> a
mygcd (a, b) = a + b 

main = do
  a <- readLn
  b <- readLn
  print $ mygcd (a, b)

2

solved Sum of two numbers with function in haskell