Functional Programming Basics What’s It All About? by Robert C. Martin (Uncle Bob) Confused about functional programming? “Uncle Bob” Martin strips the paradigm down to its essentials, and explains why you can and must understand functional programming now. By now you’ve almost certainly heard of functional programming. I mean, how could you miss it? Everybody’s talking about it. There are all these new functional languages coming out like Scala, F#, and Clojure. People are talking about older languages too, like Erlang, Haskell, ML, and others. So what’s this all about? Why is functional programming The Next Big Thing (tm)? And what in blazes is it? First, it’s almost certainly true that functional programming is the next big thing. There are good solid reasons for this that we’ll explore later in this article. But in order to understand those reasons, we need to know what functional programming is. I’m going to upset a lot of people with this next statement because I’m going to resort to extreme minimalism. I’m going to reduce functional programming down to its simplest core and this isn’t really fair because the topic is rich and expressive and full of wonderful concepts. You’ll see hints of some of them here. But for now, I’ll simply define functional programming this way: Functional programming is programming without assignment statements. Oh no! Now I’ve gone and done it. The functional programmers out there are gathering their pitchforks and torches. They want my head for uttering such minimalist blasphemy. Meanwhile all the folks who hoped to learn what functional programming really is are about to stop reading because the above statement is so blatantly absurd. I mean: how in the world can you program without assignment? The best way to explain that is to show an example. Let’s look at a very simple program in Java: the squares of integers. public class Squint { public static void main(String args[]) { for (int i=1 i=25 i++) System.out.println(i*i) } } Who hasn’t written that program, or some simple variant of it? I must have written it many hundreds of times. It’s often the second program I write in a new language, and the second or third program I teach new programmers to write. Everybody knows the good old squares of integers! But let’s look at it closely. It’s just a simple loop with variable named i that counts up from 1 to 25. Each loop through the program causes the variable i to take on a new value. This is assignment. A new value is being assigned to the variable i every pass through the loop. If you could somehow peer into the PragPub January 2013 4
Previous Page Next Page