[Haskell-cafe] Abstraction in data types

Darrin Chandler dwchandler at stilyagin.com
Thu Mar 18 01:27:33 EDT 2010


On Thu, Mar 18, 2010 at 01:06:25PM +0800, Lyndon Maydwell wrote:
> You could probably also use a typeclass for pointy things rather than
> a data type, this would then require you to use existential
> quantification to construct a hetrogenous list.
> 
> For example:
> 
> Class Point where
>     getCartesian :: ...
>     getPolar :: ...
> 
> data Shape = Point p => ... | Polygon [p]
> 
> Correct me if this is wrong though :-)

So in "normal" use Polygon list would be homogeneous, but could be made
heterogeneous with effort? If I have that right it's closer, but I'd
love to have the compiler whine if someone tried to mix them.

> On Thu, Mar 18, 2010 at 12:56 PM, Alexander Solla <ajs at 2piix.com> wrote:
> > I wrote this to Darrin, but didn't CC cafe:
> > On Mar 17, 2010, at 9:20 PM, Darrin Chandler wrote:
> >
> > type Cartesian_coord = Float
> >
> > type Latitude  = Float
> > type Longitude = Float
> >
> > data Point = Cartesian (Cartesian_coord, Cartesian_coord)
> > | Spherical (Latitude, Longitude)
> >
> > type Center = Point
> > type Radius = Float
> >
> > data Shape = Circle Center Radius
> > | Polygon [Point]
> >
> > This obviously stinks since a Polygon could contain mixed Cartesian and
> > Spherical points. Polygon needs to be one or the other, but not mixed.
> >
> > My suggestion would be to use an alternate representation of "spherical"
> > points in terms of polar coordinates, and then to normalize and mix at will:
> > type Theta = Float
> > type Radius = Float
> > data Point = Cartesian (Cartesian_coord, Cartesian_coord)
> >            | Polar   (Theta, Radius)
> > normalize_point :: Point -> Point
> > normalize_point Cartesian x y = Cartesian x y
> > normalize_point Polar t r = Cartesian x y where x = r * cos t; y = r * sin
> > t;
> > It really depends on what you want to do with your points.  If you want to
> > do linear algebra, you might want your points to depend on a basis, for
> > example.  But your "spherical" points don't really form a basis in
> > three-space, or even over all of two-space.
> > _______________________________________________
> > Haskell-Cafe mailing list
> > Haskell-Cafe at haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >
> >
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 

-- 
Darrin Chandler            |  Phoenix BSD User Group  |  MetaBUG
dwchandler at stilyagin.com   |  http://phxbug.org/      |  http://metabug.org/
http://www.stilyagin.com/  |  Daemons in the Desert   |  Global BUG Federation
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20100318/1df68745/attachment.bin


More information about the Haskell-Cafe mailing list