[Haskell-cafe] Shorthand method of enumerating a list a gotcha ... or is it just me?

Jonas Almström Duregård jonas.duregard at gmail.com
Sat May 8 04:36:29 EDT 2010


An easier way of demonstrating this issue:

Prelude> [3,7..22]::[Int]
[3,7,11,15,19]

Prelude> [3,7..22]::[Double]
[3.0,7.0,11.0,15.0,19.0,23.0]

/Jonas

On 8 May 2010 09:47, Malcolm Wallace <malcolm.wallace at cs.york.ac.uk> wrote:
>>> Hugs> [3,7..22]
>>> [3,7,11,15,19]     <- OK
>>>
>>> Hugs> map (* 1.0) [3,7..22]   <- same spec as first but !!! when
>>>                                               mapped to with a (*1.0) to
>>> coerce
>>>                                               them to reals:
>>> [3.0,7.0,11.0,15.0,19.0,23.0]   <- went one outside of range spec.
>>
>> This is because the Enum instance for floating point numbers is screwy
>> and shouldn't be used in general
>
> I think the original poster is probably being confused by the fact that the
> first mention of [3,7..22] looks identical to the second mention, but they
> are given different types by Haskell.  The literal '3', although it looks
> like an integer, is being given the type Double in the second list
> enumeration, because of the context of needing to multiply it by 1.0.  The
> comment mentions coercion, as if the list is generated at type Integer, and
> then converted to Double, but that is not the case.  It is generated at type
> Double, and no coercion happens.
>
> Regards,
>    Malcolm
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list