Guy Steele Interviews John McCarthy, Father of Lisp (2009)
infoq.com
infoq.com
Ha! I knew it! He hated LOOP as much as I do! :-)
Me for example. The by far best looping construct is Jonathan Amsterdam's ITERATE. But that is LOOP on steroids. LOOP is surprisingly practical. ITERATE is even more practical and has more parentheses. ;-) But since LOOP is there, it's good enough for a lot of purposes...
There are several objections against LOOP:
* another language. Don't use Common Lisp is embedded languages are a problem. ;-)
* the syntax. Okay.
* Spec is not good enough. For me it is good enough.
* Not enough parentheses -> ITERATE.
* non-extensible -> LOOP variants or ITERATE
I've used all kinds of stuff when it comes to loops and iterations.
Bad: tail recursion. Worse: RECUR in Clojure. Both lead to hard to maintain and hard to read code. I've one believes in Functional Programming, it might look attractive. But I don't buy that. Having read and refactored a lot of these constructs, I find them hard to maintain.
LOOP: very useful in many cases.
Series: hard to extend
ITERATE: cool, everything I want.
Btw., LOOP comes from Interlisp from the 70s and was called FOR. The idea was to have english-like embedded languages. It was then moved and extended to Maclisp, and from there to Lisp Machine Lisp and then Common Lisp.
(iterate (for elt in list-of-lists)
(finding elt maximizing (length elt)))
But what's wrong with this? (reduce (lambda (a b) (if (> (length b) (length a)) b a))
list-of-lists)
Okay, you have to do a little more work if you only want to take the length of each list once instead of twice. But if you don't care about that, this is quite elegant and introduces no new vocabulary. (Unless the lists are very long, calling LENGTH twice isn't anywhere near twice the cost of calling it once, because of cache effects.)Code like above is simply less readable. There are lots of uses of REDUCE which are simple to understand. Code like above, less so - where I need to write a special purpose function.
Another problem is that you need to think about the corner cases:
(reduce (lambda (a b) (if (> (length b) (length a)) b a))
'())
-> ErrorAlso you can see that the REDUCE version is slower. Usually there will be a function call overhead - unless the compiler has some optimization for that in some optimize level. Also 'LENGTH' is called multiple times on the longest list so far. You could work around that in REDUCE ... In ITERATE this is not needed: it calls LENGTH only once per list item.
> Code like above is simply less readable.
I write code like this all the time, and have for 35 years. I think it's perfectly readable.
So, yeah. I don't remember using it more than once in my own code.
The point, of course, is that trying to create "one macro to rule them all" is already wrongheaded. There shouldn't be one universal iteration construct.
I agree that MAPCAR et al. should be used when they're appropriate. They are, however, sometimes simply not, and it's important to be able to build code to handle those cases. You don't need LOOP for that, since you can always use TAGBODYs and GOs, but LOOP is almost certainly more appropriate, just as MAPCAR et al. are usually more appropriate than LOOP.
In other words, yes, there should be a universal iteration construct; there just shouldn't be only a universal iteration construct. There's nothing wrong with having multiple tools at different abstraction levels for the same group of tasks, and I'd argue that it's usually a good thing.
But I still think LOOP tries to be the solution to too many problems. Worse, it attempts to replace compositionality -- the rules of the language by which more complex expressions are built up from simpler ones -- with vocabulary.
Oh well. I've made my case -- I'll shut up now :-)
The thing is LOOP is very good at creating ad hoc kinds of loops. Say you are writing a loop to do password validation. Repeat at most N times and then lock the account or go through once a correct password is entered. That kind of iteration is too specific for pretty much any kind of looping construct besides LOOP.
As I see it, LOOP is not about creating "one macro to rule them all", it's about creating a macro that's a catch-all. One that you can always use when nothing else quite describes what you want.
I'm just thinking out loud.
I understand that LOOP seems simple in simple cases, but that simplicity is evidently deceptive, as attested by the comments I was replying to.
(loop repeat n
thereis (valid (read-password))
finally (lock-account))
seems simpler than (do ((i 0 (+ i 1)))
((>= i n) (lock-account))
(when (valid (read-password))
(return t)))
You are definitely right that loop is deceptively complex. In the code I gave, a reader would need to know that thereis skips the epilogue when terminating the loop (I would comment the code if it was going to be seen by others). I'm now finding it incredible how much lingo I used in the last sentence.To be able to produce a syntax abstraction like LOOP within the language is an example of mind boggling power available (excuse my minor hyperbole). But conversely one should also take it as an equally valid example of where restraint in the use of such power should be diligently exercised.
Tail-calls fit the bill. (And you can build things like reduce on top of them as normal functions.)
Although I personally use loop and eschew other iteration constructs I see the point you are getting at, it communicates better the intent.
I'm not very familiar with Lisp. Does anyone have a solid enough idea what he's talking about here to be able to give possible concrete examples?