Re: Simple recursive functions in Lisp



On Feb 9, 7:44 am, Frode Vatvedt Fjeld <fro...@xxxxxxxxx> wrote:
"Joe Marshall" <eval.ap...@xxxxxxxxx> writes:
`Recursion' and `iteration' are both special cases of the more
general concept of a fixed-point. It is kind of non-sensical to say
that one is better than the other because the salient point is that
you transfer control back to code that has already been run.
Whether you manipulate the stack or the heap on the way is largely
an implementation detail.

I don't think I understand what you mean here. I mean, everything is
an implementation detail when seen from some viewpoint that is
sufficiently distant.

I was trying to be succinct and not let myself get drawn in to the
recursion vs. iteration vs. tail-recursion debate. The point I was
trying
to make is that you can consider all function calls and loops as
`gotos that pass arguments'. In the case of a traditionally
`recursive'
function, there is an implicit continuation argument that must be
allocated on each call. In the case of `tail recursion', the implicit
continuation is re-used. Not all languages have a mechanism that
allows you to transfer control to an arbitrary function while still
re-using the continuation. Traditional `iteration' re-uses the
continuation, but generally doesn't allow argument passing. The
arguments must therefore be stored somewhere that persists
across the control transfer.

But here I understood the context to be
programmers working with (writing, reading, debugging..) code.

That's a different ball of wax. What you are used to or comfortable
with
is often a matter of taste and experience.

And
then I think it makes perfect sense to say e.g that

(defun sum (list)
(loop for x in list sum x))

is vastly better than

(defun sum (list)
(if (null list)
0
(+ (car list)
(sum (cdr list)))))

or, for better and/or worse:

(defun sum (list &optional (accumulator 0))
(if (null list)
accumulator
(sum (cdr list)
(+ accumulator
(car list)))))

Whether these are all equivalent in the eyes of a (sufficiently smart)
compiler, end-user or correctness proof, I don't see as relevant.

I guess I was making an irrelevant statement.

.



Relevant Pages

  • Re: Precedence of Logical Operators
    ... >> the sum of 1...n, because she didn't know that it was a correct ... I did the same thing with the fibonacci series when we were supposed to ... use recursion. ...
    (comp.lang.c)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... I got 79% on the test on Recursion, using C examples, at ... int example ... As a data structure, a call stack is most analogous to ... sum = sum + n; ...
    (comp.programming)
  • F.A.O. - Fons Ponsioen
    ... Shown below is the continuation of the post: ... >the report footer ... However the problem is that it now rounds up ... I only want the SUM total to round up to £10. ...
    (microsoft.public.access.reports)
  • Re: folder size
    ... Check out filer.dll (ole class "filer.fileutil"). ... seconds, while I canceled the recursion approach, ... filer results in a files collection. ... or you just have to sum up each single files size. ...
    (microsoft.public.fox.programmer.exchange)
  • Re: sum R
    ... i would like to read more about it on the internet. ... You're really so proud of realizing that the sum of 0's is 0 ... basicly that the sum over all the reals in the interval for an fwith fa fourrier series is not hard to compute. ... continuation of 0 or analytic continuation of 42 is ...
    (sci.math)