I've often thought about the relationship between music and programming. It's an interesting relationship that deserves further exploration than a simple blog post. Here I would like to present the culmination of years of casual
wondering about this topic. I'm not a cognitive scientist and therefore
not qualified to investigate the association between music and
programming at any level of expertise, but it's still something that
I'm greatly interested in learning more about.
Anyway, let me start at the beginning of my inquiries.
When I first started programming over 13 years ago I learned the standard data structures that all computer science students are taught at the university level. Computer scientists are familiar with the data structures class that all neophytes are required to matriculate. It doesn't matter what language the class is taught in; it could be Java, or Lisp, or C. For me it was Pascal, but that's besides the point.
It was curious to me as I met my fellow students and professors that many of them, like myself, were musicians. I often wondered about what the connection between music and programming may reveal about the human brain and psychology.
Opening Notes
In the curriculum, students learn data structures that allow the creation of programs that can manipulate data with efficient mechanisms. Lists and queues, sorting and searching algorithms like binary tree search, hashing algorithms are examples. This is all well and good. But fundamentally, there are three "molecules" if you will that are even more essential to computer programming than the data structures I just mentioned. These "molecules" are:
- Iteration
- Sequence
- Selection
I use the term "molecule" in an analogous manner, of course. Everyone knows that computer programs are not chemical in nature. Though, if you peel away all the layers of abstraction, all programs can be reduced to electrically charged ions stored in registers in RAM memory or on storage media like Flash drives or hard disks. Let's not go down that slope. I'm not an electrical engineer and I'm merely attempting to draw an analogy. So if we accept the analogy that these three fundamental structures are "molecules" then what are the "atoms." The "atoms" are the reserved words and declared variables that are used in programs. Reserved words are signified such as "int" for integer or "float" for floating point number that are used in the syntax of the programming language and cannot be used as variables. Typically, a programmer will assign a value to a variable such as:
int a = 3;
Where "a" is a declared variable of type integer and "3" is the value that is stored or assigned for the variable "a."
Simple enough.
Well, bear with me, there's a reason why I'm explaining what may appear as a tangent. It will all make sense by the end of this article.
Three Fundamental Structures - A Concerto!
Let's go back to the three structures I mentioned at the beginning:
- Iteration
- Sequence
- Selection
What do they mean for music and programming?
We'll take them one at a time.
Iteration - the repeating of a loop of programming code, or the repeating of a series of notes in a piece of music.
An example in programming would be to iterate over a counter as the program is searching through a database for specific keywords. The counter is used to simply keep track of the steps in the search. (I know that this is pretty basic for you computer scientists out there and I'm abstracting it quite a bit in my example for the benefit of those who are not fluent in programming)
An example in music would be a melody that is performed over and over throughout a song. The phrase:
is continually iterated over and over throughout the song "Row, Row, Row Your Boat."
Sequence - an arbitrary grouping of steps that occur in a specific order.
An example in programming would be to repeat an iteration until the keyword that is searched for is found.
An example for music are the specific notes that are played in a melody.
Selection - creates variation in a sequence or iteration. A program must select certain things based on the logic inherent in it by design. This creates variety in the program.
An example: input is received from a gamer while he is playing an adventure game. His character selects one door versus another door. There is a different outcome in the program based on the selection that the player chose.
In music we can demonstrate selection in the way a melody ends and another begins, as during a chorus (or coda). After the chorus' melody ends, the piece of music goes back to the first melody. This is selection. There may be restrictions on the available selections and this would be the key that the piece of music is written in. In programming, the programmer has limited the number of choices that are available and the number of outcomes available for a player in a game.
Selection creates
variation.
And Coming Full CircleNow, to circle back to the idea of "atoms" and "molecules," we should look at their analogies in music. The "atoms" of music are the notes or tones that musicians are familiar with and that are abstracted into dots on a sheet of paper. Molecules are then like "chords." Chords (molecules) are fundamental musical structures that are built on notes (atoms).
A CodaA computer programmer takes these fundamental constructs -- iteration, sequence, and selection -- and purposefully arranges them in a manner to instantiate programs that are compiled into applications. Several programs typically make up an application and in a sense, it's like various performers playing their parts in a piece of music; harmony.
In music, the musician uses fundamental constructs of iteration, sequence and selection and arranges them in manner that is instantiated as a piece of beautiful music presented in the aural realm.
The connections between music and programming are simply fascinating. I welcome your thoughts and comments.
Are you a programmer and also musician? Share your thoughts.
Are there other areas that might offer a similar analogy? I've recently learned about chess and fencing. Let's hear more about these other realms.
An Encore, of SortsOne of the pieces of music that intrigues me is
J.S. Bach's "
Musical Offering." In mathematical terms (and in programming too) it has a
recursive structure. This single piece of music is fascinating in it's own right, but with the added knowledge that it follows a construct familiar to computer scientists, it's even more remarkable. A complete dissertation on the relationship between recursion and Bach's "
Musical Offering" can be found in
Douglas Hofstadter's
Gödel Escher Bach.
Comments [0]