Re: better way to enumerate
- From: Kaz Kylheku <kkylheku@xxxxxxxxx>
- Date: Tue, 29 Jan 2008 21:49:32 -0800 (PST)
On Jan 29, 12:32 pm, viper-2 <visio...@xxxxxxxxxxxxxxxxx> wrote:
On Jan 29, 2:19 pm, Mark Tarver <dr.mtar...@xxxxxxxxxxxxxx> wrote:
If you're beginning Lisp so I'd advise practising your recursion
before getting involved with loop.
Mark
Yes, IMO LOOP is for blackbelts - not newbies
Yagoddabekidding.
On your way to learning about the primitive LABELS used by Pascal, you
may also consider using an optional parameter for another tail
recursive version:
(defun enumerate-with-op (start end &optional elist)
(if (> start end)
(reverse elist)
(enumerate-with-op (1+ start) end
(cons start elist))))
I'm not a newbie, yet whenever I see code like this, it helps me to
mentally transliterate it to the loop that it wants to be, e.g:
(defun enumerate-with-op-iter (start end &aux elist)
(loop
;; test terminating condition, return result
(when (> start end)
(return (reverse elist)))
;; update variables for next iteration
(psetf
start (1+ start)
elist (cons start elist)))))
A newbie will more readily understand this than tail recursion.
It's better engineering too, since it produces code that is as good as
tail recursion without requiring special compiler support, and won't
blow your stack where that support is missing.
Recursion should only be used when the problem divides into
subproblems in such a way that the recursive depth is logarithmic in
the size of the input.
Newbies should definitely not be taught obfuscated crap that can
exhaust their stack storage when they don't have the right compiler or
don't invoke it in the right way. You're bringing in way too many
issues into a beginner's lesson.
Then there is the problem that people who are forced to study stuff
like this in university tend to become prejudiced against Lisp-like
languages for the rest of their lives.
.
- Follow-Ups:
- Re: better way to enumerate
- From: John Thingstad
- Re: better way to enumerate
- References:
- better way to enumerate
- From: vijay
- Re: better way to enumerate
- From: Mark Tarver
- Re: better way to enumerate
- From: viper-2
- better way to enumerate
- Prev by Date: Re: Trouble getting Closure Web Browser to work.
- Next by Date: Re: newbie exploring better ways
- Previous by thread: Re: better way to enumerate
- Next by thread: Re: better way to enumerate
- Index(es):
Relevant Pages
|