Functional Programming

Posted by Daniel Lyons Thu, 17 May 2007 09:11:00 GMT

Functional programming is undergoing a sort of a collective raised eyebrow these days. Here’s why I think it’s cool and useful:

  1. Functional programs are more concise.

    This is because recursive definitions are closer to their mathematical counterparts than procedural ones.

  2. Functional data structures have interesting properties.

    For example, purely functional lists and trees work through an implicit copy-on-write, meaning older versions of the data structure can continue to exist very cheaply.

  3. Lazy languages improve performance through memoization.

    If all your functions have the same result for a given input, then the compiler can just substitute the answer for the whole computation after it has been computed once. This saves a lot of compute cycles at the cost of some memory.

  4. Lazy algorithms let us use high-level metaphors without sacrificing performance.

    It’s natural to talk about, for example, the Fibonacci sequence as an infinite list, but only lazy languages like Haskell let you really define it that way.

  5. Functional programs are more modular than procedural or object-oriented programs.

    Through algebraic data types, type classes, polymorphic functions and functors, you can reap almost all the benefits of OO software construction. But you also get pattern-matching and a wealth of function composition functions, and very cheap and pretty anonymous functions. Your glue code becomes small and transparent. The net effect is tremendous.

  6. Functional programs have fewer bugs.

    Functional programs depend on mathematical properties to a far greater extent than on intrinsic state. This means the method of computation is not hidden in the background in terms of correct sequencing of modifications to some binary chunk of data. Because the pieces are smaller and work deterministically with a given input, you are more confident of the result when they are combined. Higher-order functions (functions which combine other functions) are smaller and well-understood, and replace much of what would be hand-written glue code in a procedural language. All these attributes combine to make code that, in general, is more reliable than procedural or OO code.

Flame on.

Tags , ,  | no comments

Comments

(leave url/email »)

   Comment Markup Help Preview comment