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

Nicholls, Mark Nicholls.Mark at mtvne.com
Mon Dec 17 08:18:11 EST 2007

Not really with this...

The open case (as in OO) seems to be more like the Haskell class
construct, i.e. if new types declare themselves to be members of a class
then they must satisfy certain constaints....I can then specify "equals"
with the class and leave the onus on the implementor to implement
it....the data construct seems more analogous to a OO class
definition...which is closed in the same way. 
The approach is deliberate...but I accept may be harder than it needs to
be...I'm interested in Haskell because of the alleged power/formality of
it's type system against the relatively weakness of OO ones...the irony
at the moment is that they do not really seem to correspond
directly....and OO type system seems to (loosely) correlate to Haskell
type class system, and an OO class system (loosely) to Haskels type
system, though in OOP's they are unpleasantly tangled.

-----Original Message-----
From: haskell-cafe-bounces at haskell.org
[mailto:haskell-cafe-bounces at haskell.org] On Behalf Of apfelmus
Sent: 17 December 2007 12:34
To: haskell-cafe at haskell.org
Subject: [Haskell-cafe] Re: OOP'er with (hopefully) trivial

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
> 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
> 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.


Haskell-Cafe mailing list
Haskell-Cafe at haskell.org

More information about the Haskell-Cafe mailing list