[Haskell-cafe] Object Oriented programming for Functional Programmers

Alexander Solla alex.solla at gmail.com
Mon Dec 31 21:51:47 CET 2012


Since you have done a significant amount of work with Haskell, I must
presume you have a solid understanding of monads.  If so, I can suggest
reading

http://stackoverflow.com/questions/2704652/monad-in-plain-english-for-the-oop-programmer-with-no-fp-background/13656209#13656209

It is "for the OO programmer", but it does a good job of clarifying some
issues.

In particular, a "class hierarchy" is analogous to a stack of monads (made
with monad transformers).  That is to say, a class attaches 'methods' to an
'object' (i.e., a context, which might include data).  Different languages
use different monads to attach methods to objects -- for example,
JavaScript uses "prototype-based" object orientation, which is very much
analogous to the Haskell pattern:

data SomeData = ...

defaultData :: SomeData
defaultData = ...

setName :: Name -> SomeData -> SomeData
setName = ...

namedData :: SomeData
namedData = setName (Name "Pete") defaultData

Typically, an object oriented language provides an enriched IO monad which
provides method dispatch based on context, state "for free" by setting
variables (i.e., the dreaded side-effect), and so on.  From a functional
perspective, this is all syntactic sugar for a complicated monad
transformer stack:

type OO a = ClassT a (StateT a IO) a

Typical object oriented languages are procedural in nature, and use
explicit looping to handle iterating over data structures.  Typically, all
work is done in the same monad and it is "impossible" to define your own
monads using "best practices" for that OO language.



On Sun, Dec 30, 2012 at 11:58 AM, Daniel Díaz Casanueva <
dhelta.diaz at gmail.com> wrote:

> Hello, Haskell Cafe folks.
>
> My programming life (which has started about 3-4 years ago) has always
> been in the functional paradigm. Eventually, I had to program in Pascal and
> Prolog for my University (where I learned Haskell). I also did some PHP,
> SQL and HTML while building some web sites, languages that I taught to
> myself. I have never had any contact with JavaScript though.
>
> But all these languages were in my life as secondary languages, being
> Haskell my predominant preference. Haskell was the first programming
> language I learned, and subsequent languages never seemed so natural and
> worthwhile to me. In fact, every time I had to use another language, I
> created a combinator library in Haskell to write it (this was the reason
> that brought me to start with the HaTeX library). Of course, this practice
> wasn't always the best approach.
>
> But, why I am writing this to you, haskellers?
>
> Well, my curiosity is bringing me to learn a new general purpose
> programming language. Haskellers are frequently comparing Object-Oriented
> languages with Haskell itself, but I have never programmed in any
> OO-language! (perhaps this is an uncommon case) I thought it could be good
> to me (as a programmer) to learn C/C++. Many interesting courses (most of
> them) use these languages and I feel like limited for being a Haskell
> programmer. It looks like I have to learn imperative programming (with side
> effects all over around) in some point of my programming life.
>
> So my questions for you all are:
>
> * Is it really worthwhile for me to learn OO-programming?
>
> * If so, where should I start? There are plenty of "functional programming
> for OO programmers" but I have never seen "OO programming for functional
> programmers".
>
> * Is it true that learning other programming languages leads to a better
> use of your favorite programming language?
>
> * Will I learn new programming strategies that I can use back in the
> Haskell world?
>
> Thanks in advance for your kind responses,
> Daniel Díaz.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121231/5199813f/attachment.htm>


More information about the Haskell-Cafe mailing list