If you have never heard about functional programming before, I’ll have the honor to instill in you my view on it.

If you have, you might know that it’s usually defined by some of its features, such as: reification of functions, avoidance or banning of functions with side-effects, use of higher-order functions and so on. But, doesn’t that sound like a collection of random features? What’s the motivation behind them?

I’ll present you a view which, at least to me, makes that set of features seem coherent; and functional programming, like a deep and beautiful paradigm:

Functional programming attempts to reduce programs to equations.

An example

Say we have a list of real numbers and we wish to calculate the geometric mean of the positive numbers in that list.

Good old C

# include <math.h> // the caller of this function must keep track of // the length of x and pass it explicitly, // otherwise we couldn't know how long it is double geometric_mean(double * x, int length) { int n = 0; double prod = 1.0; int i; for(i = 0; i< length; i++) { if( x[i] > 0 ){ n++; prod = prod * x[i]; } } return pow(prod, 1./(double) n) }

Python

def geometric_mean(x): positives = [ e for e in x if e > 0 ] product = 1 for p in positives: product *= p return product ** (1./len(positives))

Functional Python

def geometric_mean(x) : positives = filter(lambda a: a>0, x) product = reduce(lambda a, b: a*b, 1, positives) return product ** (1. /len(positives))



“Well, that’s supposedly functional code, and it still doesn’t look like an equation” – I can hear you say

It does, we only need to change the syntax (semantics will stay the same), and the underlying mathematical structure will flourish before your eyes.

Meet APL

Yes, that’s code. It’s a language and you can actually write your next program in it. Its called APL.

This is beautiful twofold:

1- makes mathematical structure of programs explicit (so it’s easier to check its correctness) and makes code super concise (less room for bugs)

2- extends the mathematical language so that you can use it to express algorithms (if you are a mathematical savvy, think: how would you describe how to sort a list, using the standard mathematical language alone?). This was the original intent of Ken Iverson, the creator of APL. Only later were computers able to execute it. (Isn’t it weird that we have to resort to natural language or pseudo-code to express algorithms, which are inherently mathematical entities?)

thus effectively blurring the line between mathematics and code.

J

People looked down on APL for its non-ascii character set. Therefore, Ken Iverson created J, a successor of APL which restricts to the ASCII character set, and incorporates more powerful concepts along the way, such as tacit programming

Here are the 3 pythagorean means implemented in J:

arithmetic_mean := (+/ % #) geometric_mean := (+/ % #) &. ^. harmonic_mean := (+/ % #) &. %

Those are not totem-pole smileys, they are wonderfully abstract and concise tacit functions.

Conclusion

I hope I have wet your interest in functional programming. Remember you don’t need to learn APL or J to think along the lines of this paradigm, as long as you can see through the syntax of imperative languages.

(* Just in case all that wasn’t enough to convince you about the superiority of the functional gospel, take this threat: FP will displace OOP, so start thinking along our lines our you will eventually lose your job)

Compártelo: Twitter

Facebook

Like this: Like Loading...