[Haskell-cafe] Construction of short vectors

Felipe Lessa felipe.lessa at gmail.com
Fri Jun 25 17:30:06 EDT 2010


On Fri, Jun 25, 2010 at 12:41:48AM +0400, Alexey Khudyakov wrote:
> Then constructor like one below arise naturally. And I don't know how to write
> them properly. It's possible to use fromList but then list could be allocated
> which is obviously wasteful.

Did you see the generated core?  I think you should give a try to
the following simple code:

  import qualified Data.Vector.Generic as VG -- vector == 0.6.*

  vector2 :: Double -> Double -> Vec2D
  vector2 x y = Vec2D (VG.fromListN 2 [x,y])

> Another question is there any specific problems with short vectors? They could
> be just 2 elements long. I mean performance problems

Probably there will be more overhead than defining

  data Vec2D = Vec2D {-# UNPACK #-} !Double
                     {-# UNPACK #-} !Double

You should profile to see how much difference there is between
those representations.

Cheers,

--
Felipe.


More information about the Haskell-Cafe mailing list