Monday, April 02, 2007

What’s so elegant about the Common Lisp language?

Well, first of all, IMHO, it’s a uniform language. What is a uniform language? Take a look at C. A function call is like foo(bar). But in C, everything can’t be done with just functions. You gotta use the control statements. The if statement, for instance, its head looks quite like a function: if(blah), while it’s got a huge and ugly body with or without braces, one or more statements. It’s not uniform. Function calls and control statements look way different from each other. They won’t even greet each other meeting on the road, I guess.

Smalltalk is somewhat uniform, because it contains no control statements. Smalltalk is totally OO, and it actually uses polymorphism to avoid conditions. If you ever read about polymorphism-instead-of-condition talk, and got really confused or even frustrated looking at your C++ or Java code, do take a look at Smalltalk. There’s no special if keyword there. If you have to do anything depending upon a condition, use ifTrue: ifFalse: keyword method of the Boolean class. It’s not a keyword, it’s a method. If you wish, you can write your own whenTrue: whenFalse: methods and work equally well. And, the fact that they’re ordinary functions means they look exactly the same like any other functions of the language. And thus is the language uniform.

Well, I call Smalltalk “somewhat” uniform because its definition of methods looks different from the calls of the methods. Few languages actually make both sides look exactly the same. In JavaScript for example, you can define a function just like writing ordinary statements. Say, foo = function(bar) {return bar;}. It’s an assignment, so it’s a legal statement. But on the other hand, you can still define functions the traditional and declarative way: function foo(bar) {return bar;}. It does’t look like a statement and it doesn’t behave like one. The latter situation can be tolerated since we’re talking about “looking” elegant. The former part of the sentence declares the lack of elegance of the JavaScript language.

Lisp is different. It’s uniform. if is special in Lisp, but it looks just the same way as anything else. (if t (foo bar) (fooo barr)). Foo is supposed to be a function. And the if expression looks just like the invocation of the foo function. You wanna see the function declaration? No problem, (defun foo (bar) (bar)). Looks the same. Parantheses, and tokens in between. Everything is a list. What elegance!

No comments: