<div dir="ltr">Hello,<div><br></div><div>I was talking to friends about how could you make a Point type in haskell. It's intuitive to make a 2D point such as:</div><div><br></div><div>    type Point2D = (Double, Double)</div>

<div><br></div><div>Let's define one operation on this new type:</div><div><br></div><div>    add2D (x1, y1) (x2, y2) = (x1+x2, y1+y2)</div><div><br></div><div>Let's say now we want a 3D point. Then we'd be tempted to do:</div>

<div><br></div><div>    type Point3D = (Double, Double, Double)</div><div><br></div><div>    add3D (x1, y1, z1) (x2, y2, z2) = (x1+x2, y1+y2, z1+z2)</div><div><br></div><div>Although those types work great and you could make a type class so you don't have to suffix each function with 2D and 3D. It feels like we are just repeating code when defining the add function. If we want to go 4D, 5D, etc it would be more repeated code.</div>

<div><br></div><div>Using a list would be more general:</div><div><br></div><div>    type Point = [Double]</div><div><br></div><div>now we have a nice, general add function</div><div><br></div><div>    add = zipWith (+)</div>

<div><br></div><div>It's not so fun that we are able to do something like:</div><div><br></div><div>    add [2,3] [5,7,11]</div><div><br></div><div>We have no type-safety that we can only operate on points with the same dimension.</div>

<div><br></div><div>How could we address this? Can we make a general function, yet retain the type safety? I suppose maybe there's something that could be done with TH so that we automatically generate those Point2D, Point3D, etc code. I'm not sure that would be a nice path to follow, though.</div>

<div><br></div><div>[]'s</div><div>Rafael</div></div>