Great Article on Functional Programming from Defmacro
Author: Duane Johnson
16
Feb
I very much enjoyed reading about the benefits of functional programming over at Defmacro.org. The author, Slava Akhmechet, goes into some detail about several important advantages that I’ve been trying to enumerate for myself:
- Unit Testing: Since there are no reference to global variables or external state in FP, unit tests can focus precisely on the function that needs testing, without the difficulty of setting up a particular state before calling the function.
- Debugging: Again, without references to global variables or external state, debugging is simply a matter of drilling down to the problem.
- Concurrency: As a mathematical abstraction, functional programs have the potential to be reasoned about by the compilers and interpreters that run them. As a consequence, concurrency becomes a compiler option rather than a difficult-to-implement coding pattern.
- Hot Code Deployment: I would love to do this on a web server some time! No more software down time.
- Higher Order Functions: As an alternative to object-oriented programming, this feature caught my eye. I’ve been thinking in terms of closures for quite some time (thanks to Ruby) but I’m still getting used to the idea that OOP isn’t necessarily the best way to do things in every circumstance.
- Currying: This lets you build up functions from other functions. The typical example is in defining the “inc” function: it is simply the “add” function with the number “1″ bound to it for every subsequent call. No need to write a function to do that, just define “inc” in terms of “add”.
- Continuations: This aspect of FP (and other languages, such as Ruby) is fascinating. It’s very nearly an assembly language instruction, longjmp, but with the added benefit of an environment where variable state is stored. I like how Slava compares this with putting a computer to “sleep”—when the computer wakes up, it’s like nothing happened, even though time elapsed.
- Pattern Matching: Nice, but in agreement with the article, pattern matching doesn’t seem all that revolutionary. I could be wrong.
- Closures: Slava points out that this is a bridge between the OOP world and the FP world. In a special way, closures allow the formal lambda calculus to have a little exception to its strict recursive environments: closures can also access their parent environment in addition to their own.
I highly recommend this article for anyone who’s been thinking of functional programming and wants a “big picture” view. Excellent examples of real-world usage and some fun history to boot 
Leave a reply