[Haskell-cafe] Object Oriented programming for Functional Programmers

Eli Frey eli.lee.frey at gmail.com
Sun Dec 30 21:45:34 CET 2012


I think it is always a good idea to learn languages that make
<your-favorite-paradigm> hard.  There are a lot of "Aha" moments to be had
from forcing your brain to come at a problem from another angle.

As for things to watch out for.

There is a very strong duality between TypeClasses and existential
polymorphism in OO. Both require a way to dynamically look up the correct
implementation for the type you are operating upon.  In Haskell we use
Typeclasses which place this lookup table on the functions that have
existential constraints on them.

> mconcat :: Monad m => [m] -> m
> mconcat = foldl mappend []

We can think of `mconcat` having a little lookup table inside of itself,
and whenever we pass it a concrete `[m]`, `mappend` gets looked up and we
get the implementation for `m`.  Typeclasses are just mappings from types
to functions

In OO on the other hand, the lookup table is attached to the
datastructure.  We can think of the Object as a mapping from function names
to functions that operate on that Object.  Python, Javascript, Ruby, and of
course Smalltalk make this quite explicit.

Aside from Object Orientation, it is probably a good idea to learn some C
for a bit too.  C is a good language to play in and try and implement more
advanced language features.  Once you reallize that objects are just lookup
tables of functions bound with a data-structure, you can implement your own
in C, or you can make closures as functions bundled with (some) of their
arguments, or you can implement interesting datastructures, or so many
other fun things.  A good understanding of tagged unions has helped me in
many a convo with an OO head.


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/20121230/c1e00a42/attachment.htm>


More information about the Haskell-Cafe mailing list