0
votes

I am writing a function to calculate radius, and the first input is centre of a circle and second is a point from the edge of the circle.

type Coord = (Int,Int)
getRadius :: Coord -> Coord -> Float
getRadius (x0,y0) (x1,y1) = sqrt(sqrX+sqrY)
                             where sqrX = (x1-x0)*(x1-x0)
                                   sqrY = (y1-y0)*(y1-y0)

This is my code but when I compile it , an error shows that "Couldn't match expected type ‘Float’ with actual type ‘Int’. " I think the output should be a float not int . How can I fix this error? thanks

1
Frequently if one only needs to compare distances, one does not calculate the distance, but the square distance, so in such case, you could drop the sqrt.Willem Van Onsem

1 Answers

2
votes

The problem is that sqrt accepts Floating number, but fromIntegral will help to explicitly type cast the integers to floating numbers:

getRadius :: Coord -> Coord -> Float
getRadius (x0,y0) (x1,y1) = sqrt(fromIntegral (sqrX+sqrY))
                             where sqrX = (x1-x0)*(x1-x0)
                                   sqrY = (y1-y0)*(y1-y0)