how to implement accesor function for given data type

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, Martin

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!
It is not possible. What would you do with the coordinates once you had them? You wouldn't know what type of coordinates you would be getting and hence you would be unable to do anything with them. Why not something like this? getCoordinates :: Geometry -> [Coordinate] getCoordinates (Point _ c) = [c] getCoordinates (LineSegment _ (c1,c2)) = [c1,c2] getCoordinates (Polyline _ cs) = cs getCoordinates (Polygon _ cs) = cs -Brent

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
participants (2)
-
Brent Yorgey
-
Martin Tomko