# [Haskell-cafe] Abstraction in data types

Lyndon Maydwell maydwell at gmail.com
Thu Mar 18 01:06:25 EDT 2010

```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 :-)

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
>
> 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
> data Point = Cartesian (Cartesian_coord, Cartesian_coord)
> 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.
> _______________________________________________