Functional programming languages

When I first came across the functional paradigm, I thought that it sounded very cool and hip because other paradigms were too mainstream.

But when asked in several occasions to explain it, I found difficulty in conveying the essence of what it means to be a functional language. So I have decided to sit down and examine this issue.

While mainly emphasized in academia (standard ml, haskell), there have been many uses in the real world. Jane Street uses OCaml, some financial institutions use q. Many non-functional languages even support functional paradigm. In C++11 for instance, lambdas are introduced.

So what is a functional language? Firstly, a functional language is mostly pure. That is, given the same input, the function will always return the same output. This is similar to the mathematical definition of a function. Other forms of programming paradigm sometimes use function to mean a subroutine – jumping out of sequential execution to execute a block of code – and this may not be pure because the block might have some side effects such as modifying a global variable. Such pureness can lead to optimisations such as running code in parallel and memoization, which are attractive now that Moore’s law is arguably no longer true and people are looking at other ways to improve performance.

Secondly, a functional language will most likely have first class functions which means that functions are treated as values which can be returned by a function or passed around (to other functions). This is supported in most scripting languages like javascript, python, etc. Personally, I think this is a sensible way programme. Compare

for (int i = 0; i < Thing.length; i++) {doStuff(i)}

with

Thing.forEach(doStuff)

However, this comes at a cost because extra indirection is needed to treat functions as values.

I am not a big advocate of functional languages, but I think that a functional paradigm is inherently very elegant and neat (also readable) and this should be encouraged if it is the most natural way to write code for a use case.

Advertisements

Published by

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s