Re: better way to enumerate



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.
.



Relevant Pages

  • Re: knowing when it is tail recursion or not?
    ... Because then you'd get warnings on any use of recursion. ... Local tail recursion is a special case that is easy to identify, but, ... functions could potentially be turned into a loop. ...
    (comp.lang.functional)
  • Re: CAS, Lambda Calculus, Continuation and Functional Programming
    ... >>Recursion that's equivalent to loops can always be optimized back into ... and that tail calls are equivalent to loops. ... If a loop is formulated in a recursive way then ... >> that makes tail call elimination less attractive for compiler writers. ...
    (sci.math.symbolic)
  • Re: knowing when it is tail recursion or not?
    ... Because then you'd get warnings on any use of recursion. ... Local tail recursion is a special case that is easy to identify, but, ... functions could potentially be turned into a loop. ... non-tail call, but you wouldn't know if it is part of a recursion. ...
    (comp.lang.functional)
  • Re: CL Implementations and Tail-Call Elimination
    ... where they use recursion to do iteration, and usually one of the first ... at least with the right set of declarations. ... straightforward way to declare that you need tail call merging. ... this misses a programming style where a state machine can be ...
    (comp.lang.lisp)
  • Re: Response to Karen and to Willem on recursive proofs
    ... >) int nFactorial ... The bits with 'max' are because the loop is a bit ... I agree that this does not need recursion and it is an elegant ... language proofs can be used, and I need to keep the language informal ...
    (comp.programming)