# map increases length of list

9 messages
Open this post in threaded view
|

## map increases length of list

 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?
Open this post in threaded view
|

## map increases length of list

 On Wed, 17 Jun 2009 23:35:23 +0200, Aaron MacDonald <[hidden email]>   wrote: > > 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 > ----- Enumeration works differently for Double:    Prelude> [0, 60..359 :: Double]    [0.0,60.0,120.0,180.0,240.0,300.0,360.0]    Prelude> length [0, 60..359 :: Double]    7 -- Met vriendelijke groet, Henk-Jan van Tuyl -- http://functor.bamikanarie.comhttp://Van.Tuyl.eu/--
Open this post in threaded view
|

## map increases length of list

 In reply to this post by Aaron MacDonald Aaron, Look at the following snippet: > length ([0,60..359] :: [Double]) 7 Basically, the Enum instance for Double is done differently than for Int (I don't remember why exactly). If you take off the type signature, it uses Int by default. When you use map, it implicitly converts that to a Double. Hope that helps. Michael On Thu, Jun 18, 2009 at 12:35 AM, Aaron MacDonald <[hidden email]>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 > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners> -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090617/cc915786/attachment.html
Open this post in threaded view
|

## map increases length of list

 In reply to this post by Henk-Jan van Tuyl Sigh. Well, what I was trying to do was get all angles in the range [0, 360)   that have 60 degrees between them, since I'm going to be working with   hexagons.  I figured I'd use a range instead of hard-coded angles in   case I wanted to use other shapes. Thanks, Aaron On 17-Jun-09, at 6:58 PM, Henk-Jan van Tuyl wrote: > On Wed, 17 Jun 2009 23:35:23 +0200, Aaron MacDonald <[hidden email] > > wrote: > >> >> 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 >> ----- > > Enumeration works differently for Double: >  Prelude> [0, 60..359 :: Double] >  [0.0,60.0,120.0,180.0,240.0,300.0,360.0] >  Prelude> length [0, 60..359 :: Double] >  7 > > > -- > Met vriendelijke groet, > Henk-Jan van Tuyl > > > -- > http://functor.bamikanarie.com> http://Van.Tuyl.eu/> -- > >
Open this post in threaded view
|

## map increases length of list

Open this post in threaded view
|

## map increases length of list

Open this post in threaded view
|

## map increases length of list

 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 >