0
votes

I've got this error Couldn't match expected type Double -> (Double, Double, Double)' with actual type(Double, Double, Double) which I don't know how to fix, I've been struggling with this for some days with different errors and now I think I have it, just need that to get fixed, this is my code:

terna :: Double -> Double -> (Double, Double, Double) terna (x, y) = (x, y, (sqrt ((x*x)+ (y*y))))

It's simple yet I'm just starting with Haskell and find many rocks on my path due to being new to functional programming. Thank you.

2

2 Answers

4
votes

Just get rid of the parentheses and comma:

terna x y = (x, y, (sqrt ((x*x)+ (y*y))))

Function application in Haskell is done with spaces, not parentheses and commas as in most other languages. In particular, the compiler is interpreting (x, y) as a pair (tuple with 2 values), hence the type error you see.

4
votes

Take a look at the head of the function:

terna (x, y) = ...

This is the pattern of a 2-tuple, not of a function with two parameters (note that strictly speaking, functions have one parameter, here we thus construct a function with one parameter, that generates a function that then takes the other parameter, but Haskell provides a more convenient syntax for this).

As a result the signature of your function is:

terna :: (Double, Double) -> (Double, Double, Double)
terna (x, y) = (x, y, sqrt (x*x + y*y))

But typically in Haskell, functions are "curried", so it makes more sense to write it like:

terna :: Double -> Double -> (Double, Double, Double)
terna x y = (x, y, sqrt (x*x + y*y))