Why is the behavior of the Haskell range notation different for floats than for integers and chars?
Prelude> [1, 3 .. 10] :: [Int]
[1,3,5,7,9]
Prelude> [1, 3 .. 10] :: [Float]
[1.0,3.0,5.0,7.0,9.0,11.0]
Prelude> ['a', 'c' .. 'f']
"ace"
I would understand it if the last element was close to the upper bound, but this is obviously not a rounding issue.
The syntax
[e1, e2 .. e3]is really syntactic sugar forenumFromThenTo e1 e2 e3, which is a function in theEnumtypeclass.The Haskell standard defines its semantics as follows:
This is pretty much what you’d expect, but the
FloatandDoubleinstances are defined differently:I’m not really sure what the justification for this is, so the only answer I can give you is that it is that way because it’s defined that way in the standard.
You can work around this by enumerating using integers and converting to
Floatafterward.