[Haskell-beginners] map increases length of list

Jack Kennedy jack at realmode.com
Thu Jun 18 09:24:20 EDT 2009


I think I understand the reason why, but still I find it disturbing that in
this first expression, x has 6 elements:

Prelude> let x = [0,60..359]; y = [0,60..359] in (x, y, map degreesToRadians
y)
([0,60,120,180,240,300],[0.0,60.0,120.0,180.0,240.0,300.0,360.0],[0.0,1.0471975333333332,2.094395066
6666664,3.1415926,4.188790133333333,5.235987666666667,6.2831852])

But if I add a comparison to y, x now has 7 elements:

Prelude> let x = [0,60..359]; y = [0,60..359] in (x, y, map degreesToRadians
y, *x==y*)
([0.0,60.0,120.0,180.0,240.0,300.0,360.0],[0.0,60.0,120.0,180.0,240.0,300.0,360.0],[0.0,1.0471975333
333332,2.0943950666666664,3.1415926,4.188790133333333,5.235987666666667,6.2831852],True)

I. J. Kennedy


On Wed, Jun 17, 2009 at 4:55 PM, Alan Mock <docmach at gmail.com> wrote:

> That's because [0,60,..359] is not the same as [0,60..359] :: [Double].  So
> what you're passing to degreesToRadians is
> [0.0,60.0,120.0,180.0,240.0,300.0,360.0] and not [0,60,120,180,240,300].  I
> don't know why the Double version adds another number, though.
>
>
> On Jun 17, 2009, at 4:35 PM, Aaron MacDonald wrote:
>
>  For some reason, the map function returns a list that has one more element
>> than my input list.
>>
>> My input list is a range defined by [0, 60..359] (should translate into
>> [0,60,120,180,240,300]).
>>
>> The function I'm giving to map is defined this way:
>> -----
>> degreesToRadians :: Double -> Double
>> degreesToRadians degrees = degrees * (pi / 180)
>> -----
>>
>> This is how I'm calling map overall:
>> -----
>> > map degreesToRadians [0,60..359]
>>
>> [0.0,1.0471975511965976,2.0943951023931953,3.141592653589793,4.1887902047863905,5.235987755982989,6.283185307179586]
>> -----
>>
>> As you can hopefully see, there are seven elements instead of six. Getting
>> the length confirms this:
>> -----
>> > length [0,60..359]
>> 6
>> > length $ map degreesToRadians [0,60..359]
>> 7
>> -----
>>
>> I do not seem to get this behaviour with the length if I either substitute
>> the degreesToRadians function or substitute the [0,60..359] range.
>>
>> P.S. Is there a built-in function to convert degrees to radians and
>> vice-versa?
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090618/32f60c05/attachment.html


More information about the Beginners mailing list