Code switching with programming languages
When I started learning about programming languages, one of my preconceptions was that everything you know at the beginning, you remember later.
For example, one of the first things you might learn when you start with R is how to obtain the length of a vector or of a list. Let’s say we create a character vector (i.e. a bunch of wordlike things) called
many_things and then we ask how long it is, i.e. how many items it has. We use a function called
length() that returns that value. It sounds easy and obvious, if you think of functions as actions that do something, such as check the length of an item and tell you.
many_things <- c("one", "two", "three") length(many_things)
#>  3
Even if it seems obvious and easy, at first it took me a while to learn it, especially because I had no idea why on Earth I would ever be interested in the length of a vector, whatever that was. But in any case, it is a pretty basic concept, I thought.
One day, as my dad was showing me something in Python or some other language, he wanted to find the length of a list or array or something and he looked it up. My naïve self thought it was very weird that a professional would have to look up something so simple, but figured that if he knew so many languages and didn’t use all of them on a daily basis, he might forget these things. Like with any natural language as well —if you don’t speak it, some things go hybernate.
Now that I know a bit more, I see that episode in a different light. First, it doesn’t matter so much if you know which exact syntax and wording you need to obtain that information, as long as you know that you can do it and how to look it up (e.g. “length of list python”). Second, the “obvious and easy” part is not so obvious or easy once you know multiple languages, with different ways of interpreting and expressing what you want to do.
Like with natural languages. You can try to justify your prescriptivism (defining how people should use language) with a logical argument (e.g. double negatives cancel each other), but it doesn’t work because some other language (e.g. Spanish, Italian…) will use it a different way, based on some other “logic”.
For example, while in R you obtain the length of a vector or a list with
length(), in Python you obtain the length of an item with the function
len(), like so:
many_things = ["one", "two", "three"] len(many_things)
const many_things = ["one", "two", "three"]; console.log(many_things.length);
Now, if you only write in one of those languages for a while, you won’t need to look this up, but if you alternate between them, or favor one and suddenly move to the other, you might get confused when
# Python len("some string")
However, if you apply the same reasoning in R, while the result is not an error, it is certainly not what you expected.
# R length("some string")
#>  1
As it turns out, while
"some string" just has one element. In order to obtain the number of characters you need another function,
nchar(), which is vectorized, meaning it will calculate the number of characters in each of the elements of the vector, regardless of the length of the vector.
#>  11
#>  3 3 5
I find this an important point when programming is a new experience. If you know what things are possible (what you can do with a given language, which libraries are available and what they do), you don’t necessarily need to memorize how to get things done. It will come easy if you practice it, like with anything else, but it is ok to be a bit lost and look things up.