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

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

Re: how to add button to emacs that play a elisp code


From: Michael Heerdegen
Subject: Re: how to add button to emacs that play a elisp code
Date: Fri, 12 Sep 2014 03:19:05 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4.50 (gnu/linux)

Emanuel Berg <address@hidden> writes:

> How common this pitfall is in Lisp one has to wonder
> though... do you have an example?

Using quoted lists in your program, you may get a mysteriously
self-modifying behavior:


(defun 1-2-rest (&rest elements)
  "Return a list of 1, 2 and the ELEMENTS."
  (nconc '(1 2) elements))

(1-2-rest 3)
  ==> (1 2 3)

(1-2-rest 'a)
  ==> (1 2 3 a)    ;; Oops!


This happens because the list '(1 2) is constructed by the lisp reader,
and the resulting object becomes part of your program.  Compiling isn't
involved, btw.


OTOH:

(defun 1-2-rest (&rest elements)
  "Return a list of 1, 2 and the ELEMENTS."
  (nconc (list 1 2) elements))

(1-2-rest 3)
  ==> (1 2 3)

(1-2-rest 'a)
  ==> (1 2 a)    ;; Better!

Now, the list (1 2) is constructed at run-time.  This definition leads
to a quite different program.


You may argue that you can avoid destructive operations like `nconc' in
your code to prevent that problem.  But that isn't enough either: when
you pass your list to some function you didn't define yourself (e.g. a
function that is part of Emacs), it is passed as reference (as usual in
Lisp), and it may be modified by that function by side effect, so that
your program "changes" too.

If you are not aware of that problem, it probably won't bite you often,
but when it does, you'll need a lot of time to find out what's actually
going on.

BTW, this "effect" can also be used constructively, it's not "harmful"
per se.


Michael.




reply via email to

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