all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Is this a bug of Emacs-Lisp?
@ 2006-02-17  5:47 Zhang Wei
  2006-02-17 16:21 ` Giorgos Keramidas
                   ` (8 more replies)
  0 siblings, 9 replies; 13+ messages in thread
From: Zhang Wei @ 2006-02-17  5:47 UTC (permalink / raw)


(defun dummy () '(1 . 2))

(dummy)
 => (1 . 2)

(setcdr (dummy) 3)

(dummy)
 => (1 . 3)

Modify the return value of dummy changed it's defination. Is this
a bug of Elisp? If it's not. How does this happen?


-- 
Zhang Wei or Brep
<brep@smth.org>

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

* Re: Is this a bug of Emacs-Lisp?
  2006-02-17  5:47 Is this a bug of Emacs-Lisp? Zhang Wei
@ 2006-02-17 16:21 ` Giorgos Keramidas
  2006-02-18  5:26   ` Drew Adams
  2006-02-17 16:28 ` Andreas Schwab
                   ` (7 subsequent siblings)
  8 siblings, 1 reply; 13+ messages in thread
From: Giorgos Keramidas @ 2006-02-17 16:21 UTC (permalink / raw)
  Cc: emacs-devel

On 2006-02-17 13:47, Zhang Wei <id.brep@gmail.com> wrote:
> (defun dummy () '(1 . 2))
>
> (dummy)
>  => (1 . 2)
>
> (setcdr (dummy) 3)
>
> (dummy)
>  => (1 . 3)
>
> Modify the return value of dummy changed it's defination. Is this
> a bug of Elisp? If it's not. How does this happen?

You are modifying a `literal' cons cell.  It's not mandatory for the
LISP reader to create a new cons cell every time the (dummy) function
runs.

If you really want to create a new cons cell every time (dummy) runs,
you'll have to explicitly do that in the function:

    (defun dummy ()
      (copy-tree '(1 . 2)))
    => dummy

    (setcdr (dummy) 3)
    => 3

    (dummy)
    => '(1 . 2)

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

* Re: Is this a bug of Emacs-Lisp?
  2006-02-17  5:47 Is this a bug of Emacs-Lisp? Zhang Wei
  2006-02-17 16:21 ` Giorgos Keramidas
@ 2006-02-17 16:28 ` Andreas Schwab
  2006-02-17 17:20 ` David Kastrup
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Andreas Schwab @ 2006-02-17 16:28 UTC (permalink / raw)
  Cc: emacs-devel

Zhang Wei <id.brep@gmail.com> writes:

> (defun dummy () '(1 . 2))
>
> (dummy)
>  => (1 . 2)
>
> (setcdr (dummy) 3)
>
> (dummy)
>  => (1 . 3)
>
> Modify the return value of dummy changed it's defination. Is this
> a bug of Elisp? If it's not. How does this happen?

You can write FORTRAN in any language. :-) If you are destructively
modifying a constant, you'll get such results.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: Is this a bug of Emacs-Lisp?
  2006-02-17  5:47 Is this a bug of Emacs-Lisp? Zhang Wei
  2006-02-17 16:21 ` Giorgos Keramidas
  2006-02-17 16:28 ` Andreas Schwab
@ 2006-02-17 17:20 ` David Kastrup
  2006-02-17 17:57 ` Luc Teirlinck
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: David Kastrup @ 2006-02-17 17:20 UTC (permalink / raw)
  Cc: emacs-devel

Zhang Wei <id.brep@gmail.com> writes:

> (defun dummy () '(1 . 2))
>
> (dummy)
>  => (1 . 2)
>
> (setcdr (dummy) 3)
>
> (dummy)
>  => (1 . 3)
>
> Modify the return value of dummy changed it's defination.

No, it didn't.

> Is this a bug of Elisp? If it's not. How does this happen?

'(1 . 2) is a cons cell.  Cons cells are _always_ accessed by
reference, like arrays and strings.

So what is compiled into the function is a reference to a cons cell,
and this reference stays the same.  setcdr however changes the cons
cell itself.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

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

* Re: Is this a bug of Emacs-Lisp?
  2006-02-17  5:47 Is this a bug of Emacs-Lisp? Zhang Wei
                   ` (2 preceding siblings ...)
  2006-02-17 17:20 ` David Kastrup
@ 2006-02-17 17:57 ` Luc Teirlinck
  2006-02-17 18:18 ` Luc Teirlinck
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Luc Teirlinck @ 2006-02-17 17:57 UTC (permalink / raw)
  Cc: emacs-devel

Zhang Wei wrote:

   (defun dummy () '(1 . 2))

   (dummy)
    => (1 . 2)

   (setcdr (dummy) 3)

   (dummy)
    => (1 . 3)

   Modify the return value of dummy changed it's defination. Is this
   a bug of Elisp?

No.  You get the same result in Common Lisp if you replace, in the
above, setcdr with rplacd.

   If it's not. How does this happen?

If a function returns a quoted cons, then after you change the cdr of
the cons, it still returns that (now changed) cons.

Using (defun dummy (cons 1 2)) will prevent this.

Sincerely,

Luc.

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

* Re: Is this a bug of Emacs-Lisp?
  2006-02-17  5:47 Is this a bug of Emacs-Lisp? Zhang Wei
                   ` (3 preceding siblings ...)
  2006-02-17 17:57 ` Luc Teirlinck
@ 2006-02-17 18:18 ` Luc Teirlinck
  2006-02-17 19:24 ` Alan Mackenzie
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Luc Teirlinck @ 2006-02-17 18:18 UTC (permalink / raw)
  Cc: emacs-devel

Zhang Wei wrote:

   Modify the return value of dummy changed it's defination. Is this
   a bug of Elisp? If it's not. How does this happen?

>From my previous reply:

    If a function returns a quoted cons, then after you change the cdr of
    the cons, it still returns that (now changed) cons.

More precisely: you change the actual cons stored in the function cell
of the symbol `dummy'.  The function returns that stored cons each
time it is called.  To make it return a new cons each time, make it
return (cons 1 2).

Sincerely,

Luc.

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

* Re: Is this a bug of Emacs-Lisp?
  2006-02-17  5:47 Is this a bug of Emacs-Lisp? Zhang Wei
                   ` (4 preceding siblings ...)
  2006-02-17 18:18 ` Luc Teirlinck
@ 2006-02-17 19:24 ` Alan Mackenzie
  2006-02-17 19:29 ` Kevin Rodgers
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Alan Mackenzie @ 2006-02-17 19:24 UTC (permalink / raw)
  Cc: emacs-devel

Hi!

On Fri, 17 Feb 2006, Zhang Wei wrote:

>(defun dummy () '(1 . 2))

>(dummy)
> => (1 . 2)

>(setcdr (dummy) 3)

>(dummy)
> => (1 . 3)

>Modify the return value of dummy changed it's defination. Is this
>a bug of Elisp? If it's not. How does this happen?

No, it's not a bug at all.  You haven't changed the definition of dummy
with the setcdr - you've merely altered a cons cell which happened to
have been produced some time earlier by dummy.

Please also note that emacs-devel@gnu.org isn't really the place for this
sort of question - it's a mailing list for discussing the development of
Emacs.  You'd do better by posting to the list <help-gnu-emacs@gnu.org>,
or the equivalent newsgroups, gnu.emacs.help.

>-- 
>Zhang Wei or Brep
><brep@smth.org>

-- 
Alan Mackenzie (Munich, Germany)

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

* Re: Is this a bug of Emacs-Lisp?
  2006-02-17  5:47 Is this a bug of Emacs-Lisp? Zhang Wei
                   ` (5 preceding siblings ...)
  2006-02-17 19:24 ` Alan Mackenzie
@ 2006-02-17 19:29 ` Kevin Rodgers
  2006-02-17 22:34 ` Sascha Wilde
  2006-02-18 18:34 ` Richard M. Stallman
  8 siblings, 0 replies; 13+ messages in thread
From: Kevin Rodgers @ 2006-02-17 19:29 UTC (permalink / raw)


Zhang Wei wrote:
> (defun dummy () '(1 . 2))
> 
> (dummy)
>  => (1 . 2)
> 
> (setcdr (dummy) 3)
> 
> (dummy)
>  => (1 . 3)
> 
> Modify the return value of dummy changed it's defination. Is this
> a bug of Elisp? If it's not. How does this happen?

It is not a bug.

After the defun, (symbol-function 'dummy) =>
(lambda nil (quote (1 . 2)))

That is, the function binding of the symbol dummy is a list of 3 elements:
lambda, nil, and (quote (1 . 2))

That last element is the form that is evaluated to produce the result,
which itself is a list of 2 elements: quote and (1 . 2)

Evaluating (quote x) returns x, which in this case is the (1 . 2) cons
cell.  It is not a copy of x, it is x itself, which in this case is a
member of the function binding's sublist.

setcdr is a destructive operation that modifies that object, and by
side-effect, the function definition.

You can even write a self-modifiying function, by having dummy call
setcdr (or any destructive function) on its own function binding.

-- 
Kevin Rodgers

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

* Re: Is this a bug of Emacs-Lisp?
  2006-02-17  5:47 Is this a bug of Emacs-Lisp? Zhang Wei
                   ` (6 preceding siblings ...)
  2006-02-17 19:29 ` Kevin Rodgers
@ 2006-02-17 22:34 ` Sascha Wilde
  2006-02-18 18:34 ` Richard M. Stallman
  8 siblings, 0 replies; 13+ messages in thread
From: Sascha Wilde @ 2006-02-17 22:34 UTC (permalink / raw)
  Cc: emacs-devel

Zhang Wei <id.brep@gmail.com> wrote:

> (defun dummy () '(1 . 2))
>
> (dummy)
>  => (1 . 2)
>
> (setcdr (dummy) 3)
>
> (dummy)
>  => (1 . 3)
>
> Modify the return value of dummy changed it's defination.

No.  The function dummy returns a cons cell -- all ways the _same_
cons cell, so if you change it, well it ends up changed.  :-)

What you might want is:

(defun dummy () (cons 1 2))

cheers
sascha
-- 
Sascha Wilde

Lisp is the red pill.
  -- John Fraser, comp.lang.lisp

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

* RE: Is this a bug of Emacs-Lisp?
  2006-02-17 16:21 ` Giorgos Keramidas
@ 2006-02-18  5:26   ` Drew Adams
  2006-02-18  6:42     ` Drew Adams
  2006-02-18 16:18     ` Andreas Schwab
  0 siblings, 2 replies; 13+ messages in thread
From: Drew Adams @ 2006-02-18  5:26 UTC (permalink / raw)


    If you really want to create a new cons cell every time (dummy) runs,
    you'll have to explicitly do that in the function:

        (defun dummy () (copy-tree '(1 . 2)))

Sure, but that's really doing things the hard way! If you want a new cons
cell, then function `cons' is your man - no need to beat around the bush:

 (defun dummy () (cons 1 . 2))

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

* RE: Is this a bug of Emacs-Lisp?
  2006-02-18  5:26   ` Drew Adams
@ 2006-02-18  6:42     ` Drew Adams
  2006-02-18 16:18     ` Andreas Schwab
  1 sibling, 0 replies; 13+ messages in thread
From: Drew Adams @ 2006-02-18  6:42 UTC (permalink / raw)


I wrote (defun dummy () (cons 1 . 2))
but of course meant (defun dummy () (cons 1 2))

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

* Re: Is this a bug of Emacs-Lisp?
  2006-02-18  5:26   ` Drew Adams
  2006-02-18  6:42     ` Drew Adams
@ 2006-02-18 16:18     ` Andreas Schwab
  1 sibling, 0 replies; 13+ messages in thread
From: Andreas Schwab @ 2006-02-18 16:18 UTC (permalink / raw)
  Cc: emacs-devel

"Drew Adams" <drew.adams@oracle.com> writes:

> Sure, but that's really doing things the hard way! If you want a new cons
> cell, then function `cons' is your man - no need to beat around the bush:
>
>  (defun dummy () (cons 1 . 2))

Except that you want to drop the period.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: Is this a bug of Emacs-Lisp?
  2006-02-17  5:47 Is this a bug of Emacs-Lisp? Zhang Wei
                   ` (7 preceding siblings ...)
  2006-02-17 22:34 ` Sascha Wilde
@ 2006-02-18 18:34 ` Richard M. Stallman
  8 siblings, 0 replies; 13+ messages in thread
From: Richard M. Stallman @ 2006-02-18 18:34 UTC (permalink / raw)
  Cc: emacs-devel

    Modify the return value of dummy changed it's defination. Is this
    a bug of Elisp?

It is not a bug.  You modified part of the data structure in the
function definition.

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

end of thread, other threads:[~2006-02-18 18:34 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-02-17  5:47 Is this a bug of Emacs-Lisp? Zhang Wei
2006-02-17 16:21 ` Giorgos Keramidas
2006-02-18  5:26   ` Drew Adams
2006-02-18  6:42     ` Drew Adams
2006-02-18 16:18     ` Andreas Schwab
2006-02-17 16:28 ` Andreas Schwab
2006-02-17 17:20 ` David Kastrup
2006-02-17 17:57 ` Luc Teirlinck
2006-02-17 18:18 ` Luc Teirlinck
2006-02-17 19:24 ` Alan Mackenzie
2006-02-17 19:29 ` Kevin Rodgers
2006-02-17 22:34 ` Sascha Wilde
2006-02-18 18:34 ` Richard M. Stallman

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.