Couple of months ago, a friend of mine drew my attention to Functional Programming in Scala, a course on Coursera. Having worked for years with the likes of PHP, C, C++, Java and not to mention BASIC before that, I didn’t think much of Functional Programming. Truth be told, when someone mentioned Functional Programming to me, I thought he was talking about C or rather non-OOP programming which didn’t involve Objects(Yes! I admit it! I was a brash naïve 22 year old fresh with a non-CS degree!). As a matter of fact, that was my perception until I took the course (Okay, there’s honesty and then there’s beating a dead horse!).
So, for the benefit of the uninitiated who happened to accidentally stumble upon my obscure and randomly technical blog, I’ll give the lowdown on Functional Programming … in my own words. Apart from the regular differences between Functional and Imperative programming, here’s my experience with it:
- Functional Programming tries to solve a problem in a way that’s more natural to thinking and logic-flow. Ex: myList contains “apple”
- Functional Programming involves much less typing than Imperative Programming. This is because we don’t try to fit our thinking into the way the computer does things. Instead, there are several helper functions which make it easier for us to use. Ex: “succ 8” outputs 9 as the successor in Haskell
- The point above is not to be mistakenly understood as “Ah-ha! Less code implies quicker to code”. The fact is, Functional Programming usually requires more time thinking and formulating the flow than typing. In other words, your brain starts burning calories rather than your fingers. In many cases, the easiest way to find an optimal solution is with a pen and paper. There have been times when I’ve spent a whole day just to come up with 4 lines of code. And that was the answer!
- Optimizing code to run faster requires a more thorough understanding of the libraries you’re using. Since there are several ways to perform the same task, it’s quite possible that the method you’re using works well, but is taking O(n2) instead of O(n) which is available through a simple rearrangement of order or maybe a function which is already available to perform the same task.
- Lists in Functional Programming are very powerful. Way more powerful than what you’re used to in Imperative Programming. I’m very comfortable with Collections and Generics in Java, but it comes no where close to Functional Programming. As I get comfortable with Functional Programming, I find myself implementing what would normally be nested loops in Imperative languages using a single line of code in Scala. Easiest done with Lists
Forget about loops. I know this is the hardest thing to do coming from an Imperative Programming Language, but it’s true. There’s very rarely the need to use any for-loop or while-loop. Everything is done through recursion. And there are a couple of commonly used techniques that will help you get around most of the “loopy” situations. Yes, you can sort an array without a loop and you can sum to ‘n’ numbers without a loop.
These are the important aspects that I’ve experienced when using Functional Programming. Some Functional Programming Languages are Scala, Haskell, Erlang, Lisp etc. All of them have their benefits and drawbacks which I will not go into here. But for someone coming from a Java background, Scala is the handiest. Not only does it work happily in Eclipse, it also integrates bloody well with Java. By “Bloody well”, I mean Lists with generics getting exchanged seamlessly! Here is a brilliant post on how the different List types map between each other using Java converters.
There’s a lot more information available on the web about Functional Programming. I’d encourage you to take a look if you haven’t already.
If your interest has been piqued on Functional Programming, I recommend taking the Coursera course when it’s offered again if you’re keen on learning Scala. If you’d like to learn Haskell, there’s an excellent and humorous free online book called Learn you a Haskell for Great Good. I’ve read a chapter or so in it, but decided I better stick to Scala for now since I’m big on Java. In any case, the skills should be easily transferable since almost all Functional Programming languages implement the same functions.