[Haskell-beginners] Program reliability and multiple data constructors; polymorphism
Antoine Latter
aslatter at gmail.com
Wed Apr 18 18:52:46 CEST 2012
On Wed, Apr 18, 2012 at 10:10 AM, umptious <umptious at gmail.com> wrote:
> One of the programming exercises I keep evolving as I learn Haskell is a toy
> 2D shape editor with four primitives:
>
> data Shape = Circle {origin::Pt2, radius::Float}
> | Square {origin::Pt2, side ::Float}
> | Rect {origin::Pt2, other ::Pt2}
> | Composite {shapes::[Shape]}
> deriving (Show, Read)
>
> The intent is Composites can contain Shapes of any kind, including other
> Composites so that you can apply transformations to a Composite and these
> will be applied to the contained Shapes recursively. So an arm might contain
> a hand which constains a dozen or so Rects. Transform the arm and the hand
> and rects should transform; transform the hand and its rects should
> transform but the not arm. Big deal.
>
>
> ..So what is the Haskell idiom for dealing with this??? In fact I suppose
> I'm asking two questions:
>
> 1. How do I re-design this program so it is safe (use class and instance
> maybe, abandoning use of a single data type? but I then have to have
> separate Lists for each type, even if they derived from a common class?)
>
> 2. How can one use compile time checking or (less good) coding practices to
> eliminate the possibilty of such runtime exceptions?
>
I think the usual answer is to use pattern matching. As in:
myFunc :: Sahpe -> Foo
myFunc (Composite xs) = ...
myFunc (Circle o r) = ...
etc.
Does this do what you want?
Antoine
More information about the Beginners
mailing list