A mathematician might write:

3 + 5 x 4 = 23

An equivalent statement in Lisp might look like this:

(= (+ (* 5 4) 3) 23)

The former is an undeniably cleaner way of writing that equation, and it shows that the “regular” way of writing equations is far superior to the Lisp way.

Why, then, is Lisp such a cult hit? Perhaps the example above is misleading because programming is not mathematics, so let’s try it again. Here’s a possible statement one might use in a web application written in Ruby On Rails:

return false and redirect_to login_url unless user.logged_in?

And in Lisp:

(unless (logged_in? user) (and (return false) (redirect_to login_url)))

Again, the Ruby version is much cleaner than the Lisp version. Still, in order to be fair, I should point out the tremendous advantage of Lisp’s syntax; it is 100% consistent. If you didn’t know the order of operations, you might get confused in the original mathematical example I used. It could have meant:

(3 + 4) x 5 = 23 -> which is wrong

Or, it could have meant:

3 + (4 x 5) = 23 -> which is correct

Because the parentheses aren’t specifically written down, the meaning of the equation is ambiguous, which is why mathematicians have to have rules for the order of operations in order to clarify what happens first. Similarly, in the Ruby example one must know the order that the computer evaluates “and” and “unless” and “return” and so on to be absolutely certain of what the end result will be. This problem can make it hard for people to understand what’s going on, and it can be hard for the computer to parse the language as well. It can even lead to bugs in the software. In most languages, this also means that special hard-coded “operators” can act in ways that functions can’t. Although Ruby handles this pretty well, Lisp handles this perfectly. As I said before, Lisp’s syntax is 100% consistent and works the same way for everything, everywhere.

Despite the obvious technical/theoretical advantage of Lisp’s syntax, I am still going to claim that Ruby’s syntax is superior. The problem with Lisp is that it doesn’t have syntax so much as that it has NO syntax. The creators of Lisp didn’t go out of their way to create a beautiful language with lots of splendid parentheses because they thought it looked good, rather they simply left it as it was: list processing.

However, the creators of Lisp forgot that looks do matter! For example, why do we write 158 instead of CLVIII? Because over the years it became clear to everyone that the base 10 system is a superior syntax to the Roman numeral system. It’s just easier. And so too has programming syntax evolved. Assembly language is easier than writing zeros and ones, scripting languages are easier than compiled languages, and nowadays natural language syntax is easier than archaic Lisp syntax. Let’s also not forget that mathematicians themselves, arguably the smartest group of people on Earth, chose a syntax that’s easier to read (the “regular” syntax that we have now) over a syntax that’s 100% ambiguity-free (Lisp’s syntax).

The bottom line is that readability trumps almost everything else. Most people are smart enough to figure out that “and” is evaluated before “if” which is evaluated before “return”. Any other way of doing it just wouldn’t make sense. These commonsense assumptions are what make it possible to leave out unnecessary and ugly parentheses, and to allow the Ruby language to express itself in what is as close to plain English as you can possibly get.

  • Share/Bookmark

Tags: , ,

8 Responses to “Ruby Syntax VS Lisp Syntax”

  1. Ramarren says:

    > An equivalent statement in Lisp might look like this:

    And it might also look like this:
    #i(3 + 5 * 4 = 23)
    In Common Lisp at least, which allows easy and arbitrary syntax extensions, and infix expression compiler is quite possible the oldest such extension even, because everyone at first things what you have just written. But, the funny thing is, almost everyone stops using it rather quickly. After working with consistent prefix notation for some time the undeniable becomes quite deniable.

    The critical part is the use of second dimension. Most lispers wouldn’t write any but the most trivial nested expressions on the single line, because that is, indeed undeniably, completely unreadable. But where the prefix notation shines is the ability to indicate exact level of nesting with indentation, which shows the structure of the expression at the glance. The parentheses are for the machine to read and humans to write, humans will read by the indentation, which is usually guaranteed correct, because the parentheses allow the editor to generate it automatically.

  2. Tomas says:

    I think you may have overlooked a couple advantages to having no syntax. If you use Lisp extensively you may come to appreciate the regularity of the syntax in many ways. For instance, very complex macros are possible in Lisp in an almost trivial fashion, with the full power of the language there at all times, which means you can seamlessly extend the language.

    Other languages that have tried to add macros to their syntax-ridden languages have found that it’s hard and requires ugly manipulation of the AST, or pattern matching solutions without the full power of the language available to you at macro expansion time. Look at Dylan for perhaps the most successful attempt, and then compare that with the austere beauty of Lisp’s approach.

    Ruby’s (mostly very fine) syntax allows very powerful almost-macro-like creations as well, but it does have some funny limitations – like where the blocks go, how many there can be per function call, and what the hell is the difference between a block and a proc anyway? (attention: rhetorical, I know the answer)

    You can of course fairly feel that it’s still worthwhile to have a readable syntax and to give up the advantages proffered by the Lisp style. I just thought I’d submit that the simplicity of Lisp comes with other advantages than only simplicity.

  3. Joel says:

    This post is ridiculous. You are stupid and ugly.

  4. Jasper says:

    > (unless (logged_in? user) (and (return false) (redirect_to login_url)))

    This isn’t even valid lisp, wtf are you returning from? And if you’re returning from something, what does (return false) have as output?

  5. Philip says:

    You completely missed the point, Jasper. If it will make you happy, change “return false” to “don’t show page” or something. It’s still impossible to read.

  6. Alpheus says:

    I would argue that Lisp’s syntax is superior than Ruby’s, for this reason: you stated that “the creators of Lisp didn’t go out of their way to create a beautiful language with lots of splendid parentheses because they thought it looked good, rather they simply left it as it was: list processing.”

    You do not understand the history of Lisp syntax.

    When John McCarthy created Lisp, he planned on implementing two types of syntax: S-Expressions (the lists of Lisp that we know and love), and M-Expressions (Syntax expressions to be translated into M-Expressions). Users of Lisp, however, liked S-Expressions so much, that McCarthy never got around to creating M-Expressions.

    Thus, apparently there is an advantage to Lisp expressions, besides being obscure! And that advantage is the ability to manipulate S-Expressions with macros…and this ability is the result of “EVAL”, a function which McCarthy described only for academic purposes, but was written by one of McCarthy’s graduate students anyway. Thus, Lisp wasn’t planned; it just happened. Lisp wasn’t something to be created, but rather it was something to be discovered!

    And, as a mathematician, I like it that way: there is a certain axiomatic simplicity to Lisp. This simplicity gives Lisp a certain power that can only come with simplicity! The only other language that I can think of, that offers such simplicity and power, is Forth (which in many ways is the “Turing Machine” dual to Lisp’s “Lambda Calculus”).

  7. Philip says:

    Ask yourself this: if Lisp is so simple and so powerful (and it is!) then why don’t more people use it? The answer is because the syntax is horrible.

    Setting aside everything else, you cannot deny that simple truth. You can make excuses, such as about how people will learn to love the syntax, but it hasn’t happened.

    This is one of those “reality unfortunately isn’t what I want it to be” situations. People who love Lisp are kind of like the people who hate Apple: they just refuse to accept reality. Apple products don’t have this feature or that feature, but people LOVE them. Likewise, you can argue all day about how technically Lisp has a superior syntax and everybody SHOULD love using it, but the simple fact is that they DON’T.

  8. Alpheus says:

    I’m still in the process of learning to use Lisp, but one factor of resistance for me is a lack of modern, easy-to-use libraries. For things like string processing, XML parsing, sockets, 3D computer graphics, and so forth, I’m on my own–in the case of computer graphics, for example, the only library I am aware of that would seem to do what I want, only seems to work on one version of Lisp (CMUCL), and doesn’t have any documentation. I have chosen to use Lisp, though, despite these limitations. Wish me luck!

    But this is the very issue that drove Reddit from using Lisp (despite loving it very much!) and into using Python.

    We could ask a similar question of C++: if C++ is so arcane and difficult to use, then why do so many people use it? The answer in part is because it’s descended from C, and in part, because so many other people have adopted it, and in part because it’s viewed as a “close to the metal” language. (One turning point in my curiosity towards Lisp was the discovery that a Lisp program could be optimized–as Lisp–to be as fast as an equivalent C/C++ program.) I decided that I want nothing to do with it, though, if I can help it, so it limits my options in the computer software industry.

    And yes, as a mathematician, it takes some getting used to the Polish syntax! But I’m coming to appreciate its simplicity and power as well.

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>