Lots of Insipid, Stupid Parentheses

Ronie Uliana
3 min readMay 20, 2021

--

From the timeless XKCD — https://xkcd.com/297/

If you ever told anyone that you program in Lisp (or Scheme), that’s probably the second thing you’re going to hear. The first being “What? Why!?”.

Let’s reverse the joke 😬 other languages are pretty much “lots of irrelevant confusing structural junk” 😉

Here’s my “proof” to the point:

Function calls

How do you call functions in most languages?

foo()

Lisp version is not much different, at least we have the same number of parentheses:

(foo)

But things start to get interesting when we pass arguments. Compare this:

foo(blah, bleh, bloh)

to this:

(foo blah bleh bloh)

Why do we need commas to separate arguments? 🤔

The Lisp version looks less cluttered (to my eyes, at least).

Prefixed syntax and variadic arguments

Lisp uses a prefixed syntax, meaning functions are always the first argument on a call, even mathematical operators like “+” and “/”. That makes everyone frowns upon because, well… we’ve learned differently during all our lives. That doesn’t mean it’s bad, just different.

Infix mathematical operators are undeniably straightforward:

1 + 2 + 3 + 4 + 5

But the Lisp version gets better the more arguments we have:

(+ 1 2 3 4 5)

Variadic arguments mean functions can receive a variable number of arguments. That’s quite handy.

Here is a very common pattern for checking if a value is between 0 and 10.

x > 0 && x< 10

Lisp version is:

(< 0 x 10)

It also works with more arguments, if you ever need them. Obviously, it’s possible to overdo it like (< 0 x 10 y 100) for something like “x between 0 and 10 and z between 10 and 100”.

Variadic arguments are even more interesting when we don’t pass any argument, like this:

(+) <= this one gives us 0
(*) <= and this results in 1

Very handy because those are exactly the neutral elements for sum and product.

Disguised parentheses?

Languages need structure, so we “hide” it using lots of weird stuff that has the same function as a parenthesis (grouping), but is just another random symbol. Don’t believe me?

Here a typical “if”:

if (a > 0) {
this()
} else {
that()
}

And the equivalent Lisp version:

(if (positive? a) (this) (that))

(BTW, positive? is from Racket Scheme, not Common Lisp)

How many “disguised parentheses” do we have in the typical version? Oh yeah, frequently we can omit them and just live with else 🤨

Let’s go for another example (I’m using Scheme again, ok?). Here something in Javascript:

function my_sum(arg1, arg2) {
return arg1 + arg2;
}

And the equivalent in Scheme:

(define (my-sum arg1 arg2)
(+ arg1 arg2))

Again, there is a lot of “structural stuff” there that is nothing more than different symbols for the same thing. We have the pair { and } , also return , and the ancient statement finalizer ;

Every time you see reserved words, squiggly brackets, square brackets, commas, semi-colons, crazy symbols like >> , => , -> , $$ , <$> , and so on, remember that Lisp replaces all that with parentheses and nothing else.

Yes, we have lots of parentheses, and we love them for good reasons 😉

--

--

No responses yet