November 1, 2007

The Most Beautiful Code

In the book Beautiful Code: Leading Programmers Explain How They Think, Andy Oram and Greg Wilson said that Quick Sort is the most beautiful code they had ever seen and coded. Well, they coded in C. Have a look:

(define (filter list get?)
(cond ((null? list) '())
((get? (car list)) (cons (car list) (filter (cdr list) get?)))
(else (filter (cdr list) get?))))

(define (qsort list compare)
(if (null? list)
'()
(let ((pivot (list-ref list (random (length list)))))
(append (qsort (filter list (lambda (x) (< (compare x pivot) 0))) compare)
(filter list (lambda (x) (= (compare x pivot) 0)))
(qsort (filter list (lambda (x) (> (compare x pivot) 0))) compare)))))

Hm, no C code can be better, right?

New Starting

Hm, starting a blog on the University's website. Just thinking that makes me want not to (T_T). Anyway, I wanna introduce myself to those who have time to read this humble blog: I am Lam Luu, a freshman of the U. And since I started going here, I discovered a new passion of mine: Scheme (a dialect of LISP). If you don't understand the last sentence, worry not since 9/10 other people (if not all) people do not understand it anyway.
I love Scheme. Well, again, about 99/100 of CSci students (if not all) think that I am kind of crazy or something. At least Paul Graham, Eric Raymond, Richard Stallman, Alan Kay, Neal Stephenson, etc. agree with me that, amongst programming languages, Scheme/LISP is the most beautiful, the most elegant one. Paul Graham thinks that Common Lisp, which is uglier (still better than most, though) yet more powerful. I disagree with him in this. The power of a language should derive from its beauty. Hence, the more elegant and beautiful a language is, the more powerful it becomes. In other words, there is no other languages, from C to Java, from PLI to ALGO, from Assembler to Javascript, which can be as powerful, as beautiful, as elegant than Scheme.
Hence, I start this humble blog. It has one and only one purpose: to show off power of Scheme. In here, I will implement some of the most famous algorithms and features of other languages to show my friends how this infamous "Lots of Insane Stupid Parentheses" thing can be. Again, I repeat, no language can be more precise, more elegant than Scheme. So, prepare to lament about you little favorite language's ugliness.
Oh, if anyone wants to read codes in Scheme, aware of one thing: due to its preciseness, simplicity, and elegance, Scheme (and Lisp's in general) code is exceedingly dense. If you struggle with any Fortran's descendant, well, you will struggle here.