Re: newbie exploring better ways



Griff <grettke@xxxxxxxxx> writes:

Look at it this way, there are three things that can happen when you
call this function:

If the list is empty, return the list.

If the car of the list is equal to the item, return the cdr of the
list

Else cons the car of the list with the result of calling the function
with the cdr of the list

How would that look in Lisp?

Which do you like better?

Extra credit for the OP:
* Do you know about the difference between real recursion and tail recursion?
* Do you know how CL manages tail recursion?
* Do you know how the DO construct and tail recursion relate to one another?

(Aside: I just recently added some code examples in the Portuguese
Wikipedia that might or might not help illustrate these issues.
http://pt.wikipedia.org/wiki/Lisp#Fatorial
It may be possible to piece out the point even without understanding
the surrounding text, which isn't remarkably detailed on the point anyway.)

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 was actually extremely heartened to see someone taking a class actually
using DOLIST in a decent way that wasn't prone to stack overflow.
.



Relevant Pages