Skip to content
Print

Excellent Overview of Functional Programming

14
Feb
2008

I just ran into an article on DZone that totally threw me off my chair in terms of content.  It’s titled (ambitiously enough) “Functional Programming For the Rest of Us“.  Basically the article attempts to explain many of the common FP techniques in terms that imperative programmers can understand.  More importantly, it motivates these explanations with a long and rambling set of real-world reasons.  It’s extremely long, extremely involved and well worth the read.

It’s worth noting that the author claims to be presenting his explanations in Java, but is in fact devising an extension of Java to represent FP constructs more concisely.  He does say though that this is for clarity only and it is possible to write purely functional programs in Java (stateless) “…if you felt particularly masochistic.”  Personally, I wish he had stuck with the masochistic route and avoided the invention of syntax extensions (or better yet, just used Scala); but regardless, his intent is clear.  Anyway the explanations themselves are not where the real meat of the article is.

The really interesting bit is how the author motivates FP through a combination of real-world examples and lengthy reasoning.  I’ve been sold on the benefits of functional programming for a long time now, but I’ve never seen so many supporting arguments thrown into a single batch.  For those of you still skeptical about the benefits of writing side-effect-less code in Scala, this article is for you.

So grab yourself a cup of coffee and a box of donuts (it’ll take you about that long to finish the article), and slog your way through.  Trust me, it’s worth the effort.  :-)

Comments

  1. Hello Daniel,

    I also like Scala and I like your blog very much, because of your good explanations of Scala. First thank you for this link. It points to a really impressive article in content and style.

    How does Scala fit in this description of the benefits of FP, espacially “Machine Assisted Proofs and Optimizations”? Until now the JVM does not provide these things because it is not made for functional languages. This will perhaps change in the future (Java 7?) with the Da Vinci Machine. But until now: Are the functional parts of the Scala code optimized automatically in consideration of FP technics?

    Greetings, Jörg

    Joerg Gottschling Thursday, February 28, 2008 at 2:31 am
  2. Unfortunately, program provers tend to fall on their faces when dealing with mutability and the imperative tendency toward side-effects. Because Scala is a hybrid language, it doesn’t actually *forbid* side-effects, meaning that it’s hard for provers to just mathematically say things about a discrete function. This is a limitation of the mathematical process itself, not really a problem with the JVM. Any pure-functional language can be fed into a prover, regardless of its runtime.

    As I understand it, the Da Vinci Machine project is designed simply as an extension to the JVM to allow for easier and more efficient expression of non-Java languages, especially dynamic languages like JRuby. As it is part of the runtime, it doesn’t really effect a program prover’s ability to make statements about an algorithm.

    Daniel Spiewak Thursday, February 28, 2008 at 8:41 am
  3. Yes, Scala does not forbid side effects. But perhaps the compiler could mark methods as side effect free and objects a immutable. Perhaps the developer should also do this, so Scala should provide this possibiblity. But what would be the benefit? Until now the JVM does not optimize code because it is immutable – as far as I know. So could the Scala compiler use these informations and generate optimized code? Does it?

    I’ve read that the Da Vinci Machine should also support FP. Perhaps it supports some optimizations in this direction.

    Joerg Gottschling Saturday, March 1, 2008 at 4:55 am
  4. I think the only FP aspects to the Da Vinci machine are improved efficiency on dispatch. It’s possible that there will be some optimizations for provably immutable objects, but I somewhat doubt it. Very few functional languages seem to be targeting the JVM. More precisely, very few *pure* functional languages are targeting the JVM. So while I hope that they do some optimizations for FP, I doubt that it will be anything dramatic. I’m guessing we’ll see a lot more of the effort focused on dynamic languages like JRuby, Groovy and Rhino.

    Daniel Spiewak Saturday, March 1, 2008 at 10:30 am
  5. There was this article on InfoQ about the Da Vinci Machine, which I had in mind:

    “The scope of the Da Vinci project is broader in that it targets both dynamic and functional languages.”
    “The major sub-project is the implementation of dynamic invocation …”
    “… adding support for tail calls and tail recursion (via a new tailcall bytecode prefix), and tuples (using a new tagged tuple signature string).”

    May be this will boost Scala & Co in the future. :-)

    Joerg Gottschling Sunday, March 2, 2008 at 5:10 am
  6. Ah, very nice! The tail-calls alone should help a lot, though Scala already does some clever things to optimize tail recursion. Not sure if it’ll be able to actually use the runtime-level tuple type since the APIs are already designed to use TupleN[A,B,C,...]

    Daniel Spiewak Sunday, March 2, 2008 at 11:08 am

Post a Comment

Comments are automatically formatted. Markup are either stripped or will cause large blocks of text to be eaten, depending on the phase of the moon. Code snippets should be wrapped in <pre>...</pre> tags. Indentation within pre tags will be preserved, and most instances of "<" and ">" will work without a problem.

Please note that first-time commenters are moderated, so don't panic if your comment doesn't appear immediately.

*
*