Tuesday, May 4, 2010

C++ vs. Lisp

Yes, a provocative title.

In this blog post, Mr. Ebhakt decides to compare C++ and Lisp. (Go ahead and read it, I'll wait...)

Wow! It seems the original article was written by Brandon Corfman. My apologies to Mr. Corfman. Shame on you, Ebhakt.

I had to laugh. First he is disappointed that it took him eight hours to find a solution (Norvig did it in two), and his initial attempt took 220 lines of code (Norvig did it in 45). One source of conciseness was because Lisp function calls return useful values while C++ function calls typically cause side effects. Thus the Lisp expression
(format t "~a:~ {  ~a}~%" num (reverse words))
could make use of the return value from reverse whereas the C++ version
cout << num << ":"; 
for (list::const_iterator i = words.begin(); i != words.end(); ++i) 
    cout << *i; 
cout << "\n";
could not. Mr. Ebhakt also noted that ‘most of the complexity of the STL arises from its explicit use of iterators.’ Henry Baker pointed out in 1992 that iterators are “Signs of Weakness in Object-Oriented Languages”. Armed with this knowledge, Mr. Corfman “decided to rewrite all of the STL’s algorithms to accept containers instead of iterators” and “also make the algorithms (where possible) return copies of containers instead of iterators”. Furthermore, he decided “to build another layer of utility functions on top of the STL to perform common tasks such as tokenizing strings, looking up values in containers, reading from files” etc. The end result? “The new line count is 79 lines.” I wont bother quoting Philip Greenspun at this point. Mr. Corfman sums it all up: ‘C++ isn’t all that bad.’ Of course it doesn't have garbage collection, macros, and closures, but it has ‘extensive library support’ (which he just spent hours throwing away and re-implementing) ‘and of course, its premier status’ which, with an additional two dollars will buy you a large coffee. Mr. Corfman came so close to the mark and just missed it. Lisp isn't magic, it's at a sweet spot in the design space. Lisp doesn't do anything the other languages cannot, but it does what the other ones do not. It works that much harder for the programmer so the programmer can do other things (like program). Sure, a Ford Model-A isn't all that bad. It can get you where you're going and it is unsurpassed in popularity, but if you spend most of your waking hours behind the wheel, wouldn't you rather drive a Dusenberg?
After finding out that this was written by Brandon Corfman, I decided to see if his opinion had changed at all. On his Road to Lisp page, he says “Lisp still has the definite edge on the power curve. I think the clue for me is that I still find myself saying, ‘Why didn't they do this like Lisp?’”


Aaron said...

Awesome. Although, to counter the 1992 paper roughly summarized as "iterators bad", you might want to counter their sole premise with one of the many functional iterators out there:

For example, this one: http://portal.acm.org/citation.cfm?id=1159885

Daemmerung said...

Mr "ebhakt" copied the original article from one Mr Corfman. Cf here

John Cowan said...

I think it's more sad than funny (plagiarism issues aside).

Unknown said...

Its not as terse as the lisp, but the C++ could be improved. Shame to reverse just to iterate backwards, and to write the explicit loop you don't need.

std::cout << "num" << ":";

copy(words.rbegin(), words.rend(), std::ostream_iterator< int >(std::cout));

std::cout << std::endl;

Unknown said...

Now, I'm curious. What quote by Philip Greenspun are you referring to?

Tomasz Skutnik said...


Greenspun's Tenth Rule

Arcane Sentiment said...

This isn't a case of greenspunning, only of adding missing library. Corfman didn't add any language features; he only filled in the gaps in STL, so he could use C++ in what ought to be its native style.

The real lesson here is the point about the difference between what languages can do and what they do do. For this program, library usability is much more of a problem than the difference between what the two languages can do, and the advantage of CL over C++ here has little to do with CL being a Lisp.

steck said...

Lots of luck getting a $2 coffee here in Australia.

Brandon Corfman said...

Ha! This article is no longer to be found on my site, but it lives on in other people's blogs.

Here's one of the better discussions on the article by a guy who actually coded a similar program in the D language. Most people seemed to miss the part about timing themselves while coding the program from scratch, instead of rewriting my own C++ code. :)

Unknown said...

Thanks Mr. Brandon Corfman