[Haskell-beginners] how to implement accesor function for given data type

Brent Yorgey byorgey at seas.upenn.edu
Mon Sep 13 09:59:20 EDT 2010


On Mon, Sep 13, 2010 at 09:53:21AM -0400, Brent Yorgey wrote:
> On Mon, Sep 13, 2010 at 02:52:06PM +0200, Martin Tomko wrote:
> > Dear all,
> > apologies for a newbie question, but I am returning to Haskell after
> > years of hiatus and Java...
> > 
> > I have the following data type (representing geometries):
> > 
> > data Geometry = Point FID Coordinate
> >         | LineSegment FID (Coordinate,Coordinate)
> >         | Polyline FID [Coordinate]
> >         | Polygon FID [Coordinate]
> >         deriving (Show,Eq)
> > 
> > type Coordinate = (Float, Float)
> > 
> > And this is where I fail: I have no idea how to type getCoordinates,
> > in order to match against the three different combinations -> it can
> > result into a Coordinate, or a typle, or a List. I am sure it is
> > possible!

Another thought: if it is important to be able to tell which sort of
coordinates you have (single, pair, or list), you can make a new data
type, like this:

  data CoordinateSet = Single Coordinate
                     | Pair (Coordinate, Coordinate)
                     | List [Coordinate]

Now it should be no problem to write

  getCoordinates :: Geometry -> CoordinateSet

-Brent


More information about the Beginners mailing list