Re: Better mehod to generate a sequence of range?



On 26 Aug., 22:25, Deepak Surti <dmsu...@xxxxxxxxx> wrote:
On Aug 22, 6:48 pm, Yongwei Xing <jdxyw2...@xxxxxxxxx> wrote:

Hi all

Sometimes, I need a sequence of range.
I use the code below

(defun GenerateSequenceRange(n m)
    (loop for i from n to m
            collect i)
)

I want to know if is there better and faster method to do it?

--
Welcome to my blog:http://jdxyw.blogspot.com

(defun range (n m)
  (labels ((self (n &optional acc)
             (if (> n m)
                 acc
                 (self (1+ n) (cons n acc)))))
    (nreverse (self n))))

Deepakhttp://deepaksurti.com

If you would count down, you could get rid of the nreverse.

But that looks like Scheme. CL in general does by default not support
TCO.
If the compiler is in a non-TCO mode or does not support TCO you may
get a stack overflow for large ranges.

There is nothing wrong using normal iteration using LOOP like above.

Style:

* Scheme: use tail recursive functions to nicely obfuscate the code
* CL: use LOOP to write funky near natural language iterations
* CL: if you want to show more taste, then use ITERATE (optional)





.



Relevant Pages

  • Re: For Next with negative step ?
    ... 'lPos sets row position, and is used to calc the left side of result. ... the number of columns for each iteration of the loop that follows. ...
    (microsoft.public.excel.programming)
  • Re: Returning within a Number of Functions
    ... within a loop. ... I want Matlab to stop the iteration and return to the calling ... function taking the next iteration and running until the condition is ...   callingfunction ...
    (comp.soft-sys.matlab)
  • Re: LOOP blows!
    ...     clauses appear in the source. ... This clause is not combined with the ... Right, and ``People'' includes the ones implementing LOOP, apparently, ... You must use your hidden list iteration variable to set up the ...
    (comp.lang.lisp)
  • Re: Better mehod to generate a sequence of range?
    ...     (loop for i from n to m ... If the compiler is in a non-TCO mode or does not support TCO you may ... There is nothing wrong using normal iteration using LOOP like above. ...
    (comp.lang.lisp)
  • Re: Better mehod to generate a sequence of range?
    ...     (loop for i from n to m ... (defun range (n m) ... I try to, it said stack overflow. ...
    (comp.lang.lisp)