Syntax 5.2 valid for any Fractional . Int not an instance of Fractional , and cannot or should not be. How to do what when converting an arbitrary Rational to Int is not indicated.
Converting to Double from an arbitrary fraction, however, makes perfect sense (within the type range).
Your expectation is due to the presence of implicit coercion in many languages.
However, they have a cost. You must manually ensure that the entire coercive system is confluent. Haskell does not, instead of allowing the numeral literal syntax to use a type system. To convert them between you, you need to use fromIntegral to explicitly indicate the need for coercion, this avoids merging dependency and allows programmers to define new numeric types.
belowThreshold = filter (\x -> fromIntegral x < 5.2)
This is similar to using an explicit conversion in C ++, for example ((double)x < 5.2) . Although this statement only works because of default, since 5.2 can be used as a member of any Fractional , and the result from fromIntegral x is any Num , a superclass from Fractional , so fromIntegral x < 5.2 is underspecified, it just knows that it needs to compare two Fractional values ββare of the same type, and it selects Double as a reasonable default value based on the default instruction.
Also note that Int not the only Integral type, so the above method works in any list of Integral values:
belowThreshold :: Integral a => [a] -> [a]
source share