Re: Simple recursive functions in Lisp
- From: "Joe Marshall" <eval.apply@xxxxxxxxx>
- Date: 9 Feb 2007 13:33:48 -0800
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.
.
- References:
- Simple recursive functions in Lisp
- From: S. Robert James
- Re: Simple recursive functions in Lisp
- From: Kaz Kylheku
- Re: Simple recursive functions in Lisp
- From: S. Robert James
- Re: Simple recursive functions in Lisp
- From: Frode Vatvedt Fjeld
- Re: Simple recursive functions in Lisp
- From: Joe Marshall
- Re: Simple recursive functions in Lisp
- From: Frode Vatvedt Fjeld
- Simple recursive functions in Lisp
- Prev by Date: Re: I am new to Lisp and Dynamic Type Checking
- Next by Date: Re: I am new to Lisp and Dynamic Type Checking
- Previous by thread: Re: Simple recursive functions in Lisp
- Next by thread: Re: Simple recursive functions in Lisp
- Index(es):
Relevant Pages
|