Making C++ more like Lisp
Brandon Corfman figured out why his C++ program was longer than Lisp. C++ took 200 LOC, while Peter Norvig’s Lisp version took 45 LOC. The reason?
I believe most of the complexity of the STL arises from its explicit use of iterators. 99% of the time you want to iterate over the entire container in a loop, and in searches you’re interested in the value itself, not the pointer to the value. Iterators simply provide no benefit in these circumstances. My conclusion from extensive use of the STL is that iterators are mostly useless and should be abstracted away everywhere possible.
Furthermore,
C++ modifies the existing list in-place when words.reverse() is called and returns void. This means that the cout statement which prints the list to the console has to be on a separate line, because there is no return value from the function that is printable.
The solution: EasySTL, which makes list handling in C++ more Lisp-like.
The result? The revised C++ code comes in at 48 lines.
Looks like an old dog can teach a few tricks after all.
About this entry
You’re currently reading “ Making C++ more like Lisp ,” an entry on Chui's Counterpoint
- Published:
- 5.14.05 / 4pm
- Category:
- General
No comments
Jump to comment form | comments rss [?] | trackback uri [?]