Re: newbie exploring better ways
- From: K Livingston <kevinlivingston.public@xxxxxxxxx>
- Date: Tue, 29 Jan 2008 23:39:59 -0800 (PST)
On Jan 30, 12:46 am, Kent M Pitman <pit...@xxxxxxxxxxx> wrote:
The reason I mention all of this is that Griff's remarks here might lead
you into a recursive solution, and I'm not sure that's a good idea in CL
for this particular problem.
I do not see anything wrong with Griff's suggestion, or with the
recursion entailed (pun not intended). And at least when I look at it
implemented, it is clearly tail recursive (or rather tail recursive,
modulo cons, which for any decent compiler is tail recursive), any
compiler worth it's salt will clean that up nicely. Further, I think
Griff's suggestion is one of the cleanest solutions to this problem
(presuming it's a pedagogical exercise which outlaws the use of the
REMOVE, etc.)
Presuming this is a pedagogical exercise, I would assume that it's
being used to illustrate potential lisp iteration mechanisms, like CDR-
ing down a list, or a DO loop, etc. to the OP, a PUSH nested in a
DOLIST is almost never what is needed/wanted.
And as Ken acknowledges, Griff's suggestion is even cleaner than the
LOOP version (and if there's a clean LOOP solution I trust Ken to find
it), in my implementation of Griff's suggestion, it uses fewer
symbols, is easier to see what's going on, and as I said, any good
compiler should have those performing at that same efficiency,
possibly even faster with the recursive solution, because it doesn't
require a flag to signal done.
As others have pointed out, to the OP, it would be good to contrast,
the various solutions, the original (DOLIST PUSH) is pure side effect
plus a reverse, a DO with a REVERSE at the end has no side effects,
but the disadvantage of the extra traversal for the REVERSE, the LOOP
is single iteration, but has a flag, and so it's quite as functional
in style as Griff's suggestion, which is also single iteration.
.
- Follow-Ups:
- Re: newbie exploring better ways
- From: Juho Snellman
- Re: newbie exploring better ways
- From: Maciej Katafiasz
- complier optimization for DO returning REVERSE, WAS: Re: newbie exploring better ways
- From: K Livingston
- Re: newbie exploring better ways
- References:
- newbie exploring better ways
- From: vijay
- Re: newbie exploring better ways
- From: Griff
- Re: newbie exploring better ways
- From: Kent M Pitman
- newbie exploring better ways
- Prev by Date: Re: newbie exploring better ways
- Next by Date: complier optimization for DO returning REVERSE, WAS: Re: newbie exploring better ways
- Previous by thread: Re: newbie exploring better ways
- Next by thread: complier optimization for DO returning REVERSE, WAS: Re: newbie exploring better ways
- Index(es):
Relevant Pages
|
|