80 points · by saturnflyer · 14y ago · 28 comments
Note that:
As of June 21, 2012, only the first chapter is complete.It's like trying to sell me the Brooklyn bridge before it is even built.
http://news.ycombinator.com/item?id=4154484
Would be interested to hear of any other resources that do something similar -- that show specific OO code and corresponding functional takes on the same code. I find that a lot of the discussions avoid getting down to specifics, with code.
OK let me be more specific (keep in mind that I am a little bit Haskell-minded right now so some of my examples apply to generic FP but some are more for Haskell):
As far as encapsulating state goes, you have objects in one side and closures in the other. Pretty straightfoward, if you see it in practice.
As far as control-flow goes, things are kind of very oposite. FP languages tend to use pattern matching (that is, switch statements / the observer pattern) on transparent types to do branching while OO languages settled to use message passing over opaque types. These two approaches are very much opposites - pattern matching makes it easy to add new functions and harder to add new "cases" and also forces types to be transparent. OO dispatching on the other hand, makes it easy to add new cases (classes) but makes it harder to add new methods. The objects are also opaque and encapsulated. (For more on this, search for the "expression problem")
As far as polymorphism goes, things tend to be pretty different. OO languages focus on subtyping, writing code that works seamlessly with a class and its subclasses. This is good for always being able to add new subclasses without breaking old code.
FP languages, on the other hand, tend to focus more on parameric polymorphism (generics, that is, writing code that doesn't care what types its manipulating) and Haskell handles ad-hoc polymorphism with type classes. Type classes are kind of like OO interfaces, but they allow type classes to be created after the types that "implement" them but do not allow you to turn previously monomorphic code into polymorphic code (so its kind of the opposite of OO in this sense).
----
Finally, if you are willing to go really academic on this, you can check out things like Cardeli's "A theory of Objects". Computer scientists inevitably end up translating everything back into the functional calculi they are confortable with and this leads to formal FP-to-OO translations, if that is what you are looking for. Just to warn you though, it turns out that some things like support for inheritance and dynamic dispatching can seriously complicate the final models.
I could recommend a lot of things to read, (but you've probably already read them), by language designers Wadler, John Hughes, X. Leroy, Odersky, Bertrand Meyer, Rich Hickey. Also the Channel 9 videos of Meijer, Bright, and Alexandrescu talking about D and haskell were terrific.
And for books, you probably know about Pierce's TAPL, but Harper's PL book is rarely mentioned
http://www.cs.cmu.edu/~rwh/plbook/book.pdf
http://channel9.msdn.com/Blogs/Charles/Alexandrescu-Bright-M...
http://lambda.jimpryor.net/translating_between_OCaml_Scheme_...
and a debate about monads, since nobody ever debates about them
http://haskell.1045720.n5.nabble.com/Martin-Odersky-on-quot-...
Here's an excerpt:
I’ll start by teaching you just enough Clojure to be
dangerous. Then we’ll use that knowledge to embed an
object-oriented language within Clojure in a more-or-less
conventional way.
Now if that doesn't sound like an exciting start I don't know what does.having said that, in my experience (perhaps i am just dumb, or old) fp takes a while to get used to. i first looked at haskell some 16 years ago (i can still remember reading the intro while staying in a b+b in edinburgh), before using sml and ocaml, but the first time it really felt natural to me was with clojure this year. i don't think that's much to do with clojure - it just took multiple attempts before things really stuck. perhaps the biggest help was python's slow drift (despite gvr) towards fp idioms.
How many ways can a reasonable programmer write the factorial function imperatively (without using the closed form formula or any dynamic programming). A maximum of two. A for loop or use recursion.
But just look at how many ways you could write factorial in a functional programming language. http://www.willamette.edu/~fruehr/haskell/evolution.html
It is truly unfortunate when your ability to program functionally is directly proportional to how much of the standard library you have memorised. This makes functional programming much harder to learn.
edit: I don't really care if you downvote. I come here discuss programming.
The number of ways you can write something is a horrible metric to evaluate languages on. Any sufficiently clever programmer can come up with a number of bad solutions. The useful imperative techniques are more obvious to you because you have more experience with that framework. Experienced functional programmers would just as quickly narrow their list of useful solutions.
In order to be productive in a functional style, you don't need to know much of the language's standard library. You simply write small functions that can be applied in a number of areas. You'll probably end up re-inventing the wheel though, as most functional languages come with most of the essential functional constructs included in the standard library. If anything, functional programming is more beginner friendly, because you don't end up dealing with mutable state and you can easily work on the level of abstraction that you're most comfortable with (you're just composing ever more advanced functions from simpler ones).
This page is a terrible example. Most of these are not not what a "reasonable programmer" would do.
An unreasonable programmer would be able to screw up on any paradigm. You'd probably have even more ways to screw up like this with an OO language, creating classes for recursive functions, trampolines, a pattern matching class, implement an language interpreter inside your factorial implementation, etc.