[Haskell-beginners] map increases length of list

Daniel Fischer daniel.is.fischer at web.de
Thu Jun 18 09:49:02 EDT 2009


Am Donnerstag 18 Juni 2009 15:24:20 schrieb Jack Kennedy:
> 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)

You give no type for x, so the default is chosen, that is Integer.
The expression map degreesToRadians y forces y to have type [Double] (you could give y a 
more general type if you specified a type signature).

> ([0,60,120,180,240,300],[0.0,60.0,120.0,180.0,240.0,300.0,360.0],[0.0,1.047
>1975333333332,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.2
>831852],True)

Now, the expression x==y forces x to have the same type as y, which still is [Double].

But, lo and behold:
Prelude> let degreesToRadians :: Double -> Double; degreesToRadians d = d*pi/180
Prelude> let x :: (Num a, Enum a) => [a]; x = [0, 60 .. 359]; y :: (Num a, Enum a) => [a]; 
y = [0, 60 .. 359]
Prelude> (x,y,map degreesToRadians y, x == y)
([0,60,120,180,240,300],[0,60,120,180,240,300],
[0.0,1.0471975511965976,2.0943951023931953,3.141592653589793,4.1887902047863905,5.235987755982989,6.283185307179586],True)


>
> I. J. Kennedy
>



More information about the Beginners mailing list