[Haskell-cafe] Abstraction in data types

Alexander Solla ajs at 2piix.com
Thu Mar 18 00:56:14 EDT 2010


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100318/fd332c58/attachment.html


More information about the Haskell-Cafe mailing list