[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
> 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
>
>
More information about the Haskell-Cafe
mailing list