问题描述:

I was trying to pop an element of a list in elisp as follows,

(pop '(1 2))

but, due to my misunderstanding, that doesn't work b/c the list hasn't been internalized as a symbol. Is there an idiomatic way to do the above, or is that not a proper approach? Messing around a bit further I found I could do

(pop (progn (setq tmp '(1 2)) tmp))

but it doesn't seem right. Is there a way to make anonymous lists and modify them in place like I was trying to do?

网友答案:

pop is a macro which modifies the value of its argument, a place.

E.g.,

(defparameter *var* '(1 2 3))
(pop *var*)
==> 1
*var*
==> (2 3)

Note that what gets modified is the value of the place, not the object contained in the place.

E.g.,

(defparameter *var-1* '(1 2 3))
(defparameter *var-2* *var-1*)
(pop *var-1*)
==> 1
*var-1*
==> (2 3)
*var-2*
==> (1 2 3)

IOW, the list (1 2 3) is not modified, only the value of the variable is.

What exactly are you trying to do?

网友答案:

pop doesn't do what you think it does. It doesn't modify any lists. It simply rebinds the given variable with the list's cdr.

Running (macroexpand '(pop foo)) in Emacs, I get:

(car-safe (prog1 foo (setq foo (cdr foo))))

Notice the absence of any list-modification functions such as rplaca or rplacd.

相关阅读:
Top