emacs-devel
[Top][All Lists]
Advanced

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

Re: Is this a bug of Emacs-Lisp?


From: Kevin Rodgers
Subject: Re: Is this a bug of Emacs-Lisp?
Date: Fri, 17 Feb 2006 12:29:15 -0700
User-agent: Mozilla Thunderbird 0.9 (X11/20041105)

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





reply via email to

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