Skip to content

R’s functional mission

January 31, 2024

From the start, one of the design preferences of R is to be faithful to the functional programming paradigm. One such philosophy is to make variables immutable. Mutability can produce confusion when the programmer is debugging.  Here is an example where R implements that policy of immutability when it comes to aliasing. Aliasing is a technique of naming a variable w another variable with the behaviour of one mimicked in the other. Implied aliasing does not work in R. You will have to explicitly call into your variables aliasing if you want that to happen. We compare how R prevents this compared to Python.

Let’s create a vector first, which we will turn to a list.

Let’s check its type, then turn it to a list, after that check its contents.

Let’s now assign v to ww.

Now let’s target that “f” sitting in position [[2]] to 2 as after all that seems to be what is intended.

We can see that there was no change that happened to ww.

Let’s do this in Python. Here we use L for v and LL for ww.

We can see here that we changed LL but this was side-effected to L. In functional programming, side-effects are to be avoided. Sometimes, for an intended result, side-effects may be desirable for convenience but it has the tendency of messing up the train of thought of the programmer not familiar with the program that has it.

No comments yet

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.