← Tutorials / docs

What is Functional Programming - A short informal intro

WORK IN PROGRESS !!!!!

So if you know the answer to this question skip to the next section as this will bore you.

Since you are still here, I guess you have never/only briefly used a functional programming language so let me explain some basics. Most programming languages are imperative programming languages, some examples would be C/C++, Java or Python. Imperative languages center around statements, where each statement can modify some state. Say you have the following C code:

int f(int x) {
    int arr[] = {1,2,4,8,16};
    int gres = g(arr);
    return arr[0];
}

Each statement is separated by a ;. int arr[] = {1,2,4,8,16}; adds the array to the function local state, int gres = g(arr); passes the array to the function g and return arr[0]; returns the first element from the array. Crucially the array might be changed by g and the result of f could differ from 1, depending on g. Note that in many imperative languages it is not that trivial to create such "tricky" behavior (its C after all... ), but in generally it is always possible.

Functional programming languages offer a different approach, they don't center around statements but rather expressions. Of course expressions in functional programming languages are much richer compared to their counter parts in imperative languages. So lets have a look at similar piece of code in intlang:

let f = ( \x. ( 
    let arr = vec[1,2,4,8,16] in (
    let gres = g arr in (
    vecget[arr,0] ))))

Does not even look that different right. So lets dissect this. I have put lots of parenthesis in, that hopefully make the structure of the code clearer, but non of them are needed. let (top level / global) or let in (local) expressions bind some expression to a variable. So looking at let f = (...) this does bind f to what ever expression is .... In the example this expression starts with a lambda \x.. Lambda expressions functions ie. \x. ... will when some applied to some argument calculate what ever expression ... is with x bound to the argument provided. Then we continue with let arr = vec[1,2,4,8,16] in (...) and guess what this binds the new vector (basically the same as an array) to the variable arr and then calculates what ever ... is. g arr will apply g to arr (calling a function). And finally vecget[arr,0] is simply the "innermost" expression that will fetch the first element of arr and will also be the result when f is called.

Crucially g can not change arr and the result of f is always 1. Again, I want to note, not all functional language guarantee this. For example an array in Ocaml is a mutable type and can just like in C differ after calling g.

If you still feel as confused about what a functional programming language is as before, don't worry. I would have probably felt the same way, would this have been my introduction. I think in general the best way to learn some thing is to try it and then think about it, so lets do this :)