Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Types don't match

I just tried to write a simple function to calculate the average of the input-Ints:

avg :: Int -> Int -> Int -> Float
avg x y z = (x+y+z)/3

When I exchange the signature to

avg :: Float -> Float -> Float -> Float

it works fine, but with the one above I get the following error message:

Couldn't match expected type 'Float' with actual type 'Int'.

Which possibilites do I have to use the first signature, which accepts Ints (only)?

like image 285
user1000742 Avatar asked Oct 18 '11 09:10

user1000742


1 Answers

Use fromIntegral to convert the Ints to Floats:

avg :: Int -> Int -> Int -> Float
avg x y z = (fromIntegral x + fromIntegral y + fromIntegral z) / 3
like image 67
dave4420 Avatar answered Oct 14 '22 09:10

dave4420