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)?
Use fromIntegral
to convert the Int
s to Float
s:
avg :: Int -> Int -> Int -> Float
avg x y z = (fromIntegral x + fromIntegral y + fromIntegral z) / 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With