Infix vs. Prefix Notation, from the interpreter’s POV

Before venturing into Clojure-land I read an excellent article called Lisp as the Maxwell’s Equations of Software. The Maxwell’s Equations are a few mathematical statements from which we can derive the rules of electromagnetism. The author compares this complex behavior arising from simple rules to the simple logic underlying Lisp languages. Here’s one example of that simplicity.

Prefix VS Infix notation

Most programming languages (and mathematical operations) look like this:

>> 1 + 2

This is called infix notation, and it’s the way we’re used to seeing operations laid out. But there’s nothing about these operations that require them to be written this way. In fact, there are real benefits to doing things a bit differently. Lisp languages use prefix notation:

>> (+ 1 2)

It looks pretty crazy at first, but it’s just a different way of ordering the same operators. The arguments 1 and 2 are being passed to the function +. It turns out that if the first element of a list is always a function, and the rest of the items are always its arguments, it becomes much much simpler to interpret the language. All the interpreter needs to do is, for each set of parentheses, use the first element as the function, and pass the rest of the elements to it. That sounds obvious, but imagine writing the logic for interpreting in a language that allows this:

>> 1 + 2.+(3)
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.