help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is this use of nreverse a good practice?


From: Pascal J. Bourguignon
Subject: Re: Is this use of nreverse a good practice?
Date: Sun, 01 Feb 2015 14:02:02 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Marcin Borkowski <mbork@wmi.amu.edu.pl> writes:

> Hi there,
>
> I'm writing a small function which should build a list and return it.
> What I'm doing is more or less this:
>
> (defun myfunc ()
>   (let ((list-var))
>     (while CONDITION
>       (DO-SOMETHING)
>       (push SOME-ELEMENT list-var))
>     (nreverse list-var)))
>
> A more concrete example:
>
> (defun reverse-range (n)
>   (let ((list-var))
>     (while (> n 0)
>       (push n list-var)
>       (decf n))
>     (nreverse list-var)))
>
> (reverse-range 5)
>
> ⇒ (5 4 3 2 1)
>
> Is this usage of `nreverse' correct?  I guess it is, since list-var is
> only a temporary variable and destructing it doesn't bother me.
> However, I'm never sure I'm doing things right when using
> nconc/nreverse, so I though I'd better ask...

Yes, this is THE use case for nreverse.

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


reply via email to

[Prev in Thread] Current Thread [Next in Thread]