I like Lisp’s prefix syntax. It’s consistent, has natural structure, and makes code-manipulation macros possible. But it’s not always the easiest to read or write. For example, I often want to apply several successive transformations to the same chunk of text. In Perl, I could use the default variable
$_ and then just write a bunch of regular expressions:
s/this/that/g; s/old/new/g; s/foo/bar/g;
Very succinct, but a tad cryptic. But the equivalent in Common Lisp, using the CL-PPCRE regular expression library, is much worse:
(regex-replace-all "foo" (regex-replace-all "old" (regex-replace-all "this" string "that") "new") "bar")
regex-replace-all function puts the original string in between the regex and replacement string in its argument list, which makes the syntax awkward. I usually avoid writing nested expressions like the one above and instead factor each replacement out into a separate function:
(defun replace-foo (string) (regex-replace-all "foo" string "bar")) (defun replace-old ...) (defun replace-this ...) (replace-foo (replace-old (replace-this string)))
But who wants to define three extra functions just for one expression?
Now I’m exploring Ruby, and was pleased to find how easy it is to write this:
This is succinct and reads easily from left to right. This sort of procedure is where the classic object.method(arguments) syntax really shines. At least for me, it makes sense because it’s how I tend to think about a problem: “Take this object, do this to it, then do something else to it, then give me back the result.”
The trouble I have with prefix syntax is that it feels backwards. To read Lisp code, even my own, I have to dig through the parentheses to find the innermost expression, then work my way back out again. Of course, that’s basically what a Lisp interpreter or compiler does.
I like to think it would be possible to combine the flexibility of Lisp’s S-expressions with the left-to-write readability of object.method, but I don’t know what that would be. I have little experience with Forth-style postfix syntax, but it seems even less readable. But I think this just goes to show that syntax does matter.