Functional Programming Languages, Code Readability and Multi-core Processors

Two of my computer science courses this year have to do with learning functional programming languages. The other day (actually about a month ago, this post has taken me a while to write as I've got plenty of school work to do and that's taken precedence) one of my professors mentioned that first year computer science courses were now teaching Scheme instead of the Java I learned at the time. More and more I come across mentions of Functional Programming Languages (FPLs) and how they're the next big thing. But why? And what makes them so different from the Java and C++ and other imperative programming languages (IPLs) that I've learned?

It used to be the case that hardware was expensive and programmers were cheap. So the greater your code was optimized the better the end result. This gave rise to C, C++, etc. which were absolutely great for writing code that used as little memory and processing time as possible. Unfortunately, this came at a sacrifice of Code Readability. Take this comparison of Quicksort written in C++:

int partition(int a[], int p, int r) {
int x = a[r];
int j = p - 1;
for (int i = p; i < r; i++) {

if (x <= a[i]) {
j = j + 1;
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
a[r] = a[j + 1];
a[j + 1] = x;

return (j + 1);
}

void quickSort(int a[], int p, int r) {
if (p < r) {
int q = partition(a, p, r);
quickSort(a, p, q - 1);
quickSort(a, q + 1, r);
}
}


With Quicksort writen in Haskell, a purely functional programming language.

quicksort:
qsort [] = []
qsort (x:xs) = qsort (filter (<= x) xs) ++ [x] ++ qsort (filter (> x) xs)


Even with no knowledge of Haskell and just a basic knowledge of functions you can see that what it does is map an empty domain to an empty range, and for all other cases recursively sort the smaller and larger halves and then concatenate that list. Now try and work your way through the C++ version for the first time (one of my earlier compsci classes took almost an entire 1 hour lecture to do this).

Today hardware is cheap, our processors are lightning fast, while software developers are getting payed better (at least in the US). So with less time spent writing code, you have more time for debugging (which becomes quicker because your code is that much more readable) and you can release your software faster, meaning your company is ahead with the newest thing and makes more money.

We have nearly reached the physical limits of how small we can make computer chips. The big thing now in chip design is multi-core. Everything is duo this or quad that, the PS3 even has some 8 cores. The challenge now is making use of all these cores. Code doesn't naturally know when it can be split into parallel tasks so developers wanting to take advantage of these new multi-core chips are having to pour over their old code and figure out what can be made parallel and what needs to stay serial.

So again, code readability comes into play as people go over the old things and try to parallelize them. The other big challenge with parallelization in IPLs is managing side effects. Parallel processes will need access to the same bits of data but if one process changes a bit of data and doesn't tell the other processes then those parallel process will not only give out the wrong solution but really strange solutions that don't make any sense when reading over the code. So imperative languages have things like locks and semaphores to guard data.

But then there are FPLs. A purely functional language doesn't need these concurrency controls. Why? Because of referential transparency. FPLs guarantee that given the same input, the same output will always be returned. Any function in an FPL has to have all needed data passed in. When it passes back its output, that's it. Only the output comes back nothing else has changed, no side effects.

Anyways, just some things to think about. And a note, I'm not saying FPLs are necessarily better than IPLs just that they have certain advantages that should be noticed more rather than giving all the attention to more common languages like C++ and Java. But FPLs also come with some concepts that can be hard to rap your head around like monads, the way FPLs handle I/O and state changes without introducing side effects. And if you don't like recursion, it's probably not for you, these languages are all about recursion.

0 comments:

Post a Comment