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)





.