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

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

Re: replacing a certain element in a list with another


From: Barry Margolin
Subject: Re: replacing a certain element in a list with another
Date: Tue, 30 Sep 2003 20:01:22 GMT

In article <m3u16u5dyn.fsf@tfkp07.physik.uni-erlangen.de>,
Roland Winkler  <Roland.Winkler@physik.uni-erlangen.de> wrote:
>Kind of a related question:
>
>The function `reverse' does not alter the original argument LIST. On
>the other hand, `nreverse' reverses LIST by modifying cdr pointers.
>Why does the docstring of `reverse' say
>
>  See also the function `nreverse', which is used more often.

One of the most common uses of reversing a list is part of the idiom for
accumulating values during a loop, e.g.

(let ((result nil))
  (dotimes (i 10)
    (push i result))
  (nreverse result))

Since this is a newly constructed list that has no other references to it,
it's fine to reverse it destructively rather than waste space creating a
new list.

>Yet, the info page for `nreverse' says
>
>  To avoid confusion, we usually store the result of `nreverse' back
>  in the same variable which held the original list:
>
>Wouldn't it then be more natural to use `reverse' from the
>beginning? Or am I missing something here??

You have to do the assignment whether you use reverse or nreverse.

-- 
Barry Margolin, barry.margolin@level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


reply via email to

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