Learn You A Haskell provides an example of calculating your BMI.
bmiTell :: (RealFloat a) => a -> a -> String bmiTell weight height | bmi <= skinny = "You're underweight, you emo, you!" | bmi <= normal = "You're supposedly normal. Pffft, I bet you're ugly!" | bmi <= fat = "You're fat! Lose some weight, fatty!" | otherwise = "You're a whale, congratulations!" where bmi = weight / height ^ 2 (skinny, normal, fat) = (18.5, 25.0, 30.0)
When I tried to do this example myself, I used (Num a) => a -> a -> String as the type signature for the method. However, this generated the following error:
Could not deduce (Ord a) arising from a use of '<=' from the context (Num a) bound by the type signature for bmiTell :: Num a => a -> a -> String at scratch.hs:96:12-38 Possible fix: add (Ord a) to the context of the type signature for bmiTell :: Num a => a -> a -> String
I could not resolve the error using only the Num and Ord classes. Why do I need to use RealFloat typeclass to make this piece of code work? What is special about RealFloat that is not covered by Num ?
source share