all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* newbie elisp question (nreverse or reverse)
@ 2007-05-21 16:50 Seweryn Kokot
  0 siblings, 0 replies; 2+ messages in thread
From: Seweryn Kokot @ 2007-05-21 16:50 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

Does it really matter if I use nreverse or reverse in the following
example?

(setq bla '("a" "b"))
(setq bla (append bla '("c")))

(setq bla (reverse bla))
or
(setq bla (nreverse bla))

regards,
Seweryn

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: newbie elisp question (nreverse or reverse)
       [not found] <mailman.918.1179766231.32220.help-gnu-emacs@gnu.org>
@ 2007-05-21 21:39 ` Pascal Bourguignon
  0 siblings, 0 replies; 2+ messages in thread
From: Pascal Bourguignon @ 2007-05-21 21:39 UTC (permalink / raw)
  To: help-gnu-emacs

Seweryn Kokot <s.kokot@po.opole.pl> writes:

> Hello,
>
> Does it really matter if I use nreverse or reverse in the following
> example?
>
> (setq bla '("a" "b"))
> (setq bla (append bla '("c")))
>
> (setq bla (reverse bla))
> or
> (setq bla (nreverse bla))

Yes.

(defun f ()
  (let ((blah '(a b)))
     (setf blah (append blah '(c))) ; here no problem, blah is copied, 
                                    ; but not '(c)!  
     (setf blah (nreverse blah))    ; ERROR! '(c) is modified!
     blah))

(list (f) (f)) --> ((c b a) (a b c b a))  ; !!!!

(progn (pprint (symbol-function 'f)) (terpri) nil)
prints:
(lambda ()
  (let ((blah '(a b)))
    (setf blah (append blah '(c b a)))  ; The program has been modified!!!
    (setf blah (nreverse blah))
    blah))
--> nil

So, never use destructive functions on structures that are not
mutable: make a copy first, or build mutable structures from the
start, or just use the non-destructive functions, which makes the copy
automatically.

(defun f ()
  (let ((blah '(a b)))
     (setf blah (append blah (copy-list '(c))))
     (setf blah (nreverse blah))  ; ok, because blah is a freshly consed list.
     blah))

(defun f ()
  (let ((blah '(a b)))
     (setf blah (append blah '(c)))
     (setf blah (reverse blah))  ; always ok.
     blah))

Of course, if the tail comes from an argument, you cannot use
nreverse, since you never know whether the client will call you with
'(c) or with (list 'c), then always use reverse in this case:

(defun f (c)
  (let ((blah '(a b)))
     (setf blah (append blah c))
     (setf blah (reverse blah))  ; always ok.
     blah)


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-05-21 21:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.918.1179766231.32220.help-gnu-emacs@gnu.org>
2007-05-21 21:39 ` newbie elisp question (nreverse or reverse) Pascal Bourguignon
2007-05-21 16:50 Seweryn Kokot

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.