[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: why tail recursion matters and why Java isn't it, was Re: lispperformance was Re: problems with lisp
>>>>> "Pascal" == Pascal Costanza <email@example.com> writes:
Pascal> You seem to suggest that recursion is always the most "natural"
Pascal> solution but that's just not true. In many cases, an iteration
Pascal> expresses much more clearly the intention of a programmer. Here are
Pascal> three Common Lisp versions of an example of a collector I repeatedly
Pascal> need in a current project:
Pascal> (loop for element in list
Pascal> do collect (car element))
Pascal> I am strongly convinced that the first version is objectively the best
Pascal> because it is much simpler and direct than the others. For the
Pascal> recursive version you even need to define a function. (Unless you want
Pascal> to use a Y combinator instead, of course.)
Pascal> I don't need a "fundamental rewrite" here to get efficient code,
Pascal> because the straightforward version already produces efficient code.
It seem pretty far-fetched to me to call this code "straightforward"
or "direct": You're relying on a very specialized feature of the CL
LOOP macro here to get your work done. The less you know about LOOP,
the harder it is to understand your code. (How do I know, for
example, that your actually consing up the result, and not, say,
adding it up?)
Matthias's point is that, while you can handle some special cases with
special constructs, as in this case, you can't cover all of them with
a finite number of constructs.
... and, of course, I can't resist pointing out that iteration is a
special case of recursion. So the terms you use are inappropriate.
Matthias is talking about *looping constructs*, not iteration.
Recursion is not always the most "natural" the solution---in the
spectrum you describe, it's always the *only* solution.
Cheers =8-} Mike
Friede, Völkerverständigung und überhaupt blabla