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

Martin Tomko martin.tomko at geo.uzh.ch
Mon Sep 13 08:52:06 EDT 2010

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!

class Geoms a where
    getCoordinates :: a -> b

instance Geoms Geometry where
     getCoordinates (Point _ a) = a
     getCoordinates (Polygon _ a) = a

And I want to access the Coordinates for each given geometry. Note that 
I tried to define it using records:  say, Polygon 
{getID::FID,gcoords::[Coordinate]}, but the coords function failed to 
match, due to having different return types for Geometry.

Thank you very much,

More information about the Beginners mailing list