[Haskell-cafe] Re: OOP'er with (hopefully) trivial questions.....

apfelmus apfelmus at quantentunnel.de
Mon Dec 17 07:33:55 EST 2007

Nicholls, Mark wrote:
> data Shape = Circle Int
>              | Rectangle Int Int
>              | Square Int 
> Isn't this now "closed"...i.e. the statement is effectively defining
> that shape is this and only ever this....i.e. can I in another module
> add new "types" of Shape?

Yes, but in most cases, this is actually a good thing. For instance, you 
can now define equality of two shapes:

   equal :: Shape -> Shape -> Bool
   equal (Circle x)        (Circle y)        = x == y
   equal (Rectangle x1 x2) (Rectangle y1 y2) = x1 == x2 && y1 == y2
   equal (Square x)        (Square y)        = x == y

In general, the "open" approach is limited to functions of the form

   Shape -> ... -> Shape / Int / something else

with no Shape occurring in the other ... arguments.

> I'm trying to teach myself Haskell....I've spent a few hours going
> through a few tutorials....and I sort of get the basics...
> [...]
> After many years of OOP though my brain is wired up to construct
> software in that 'pattern'....a problem for me at the moment is I cannot
> see how to construct programs in an OO style in Haskell....I know this
> is probably not the way to approach it...but I feel I need to master the
> syntax before the paradigm.

This approach is probably harder than it could be, you'll have a much 
easier time learning it from a paper-textbook like


After all, it's like learning programming anew.


More information about the Haskell-Cafe mailing list