by shigemk2

当面は技術的なことしか書かない

リスト要素を更新 削除する

add-to-listより短く、また汎変数も利用可能。

(setq l nil)
(push 1 l)				; => (1)
(macroexpand '(push 1 l))		; => (setq l (cons 1 l))
l					; => (1)
(push 2 l)				; => (2 1)
l					; => (2 1)
(cons 2 l)				; => (2 2 1)
l					; => (2 1)
(pushnew 2 l)				; => (2 1)
(macroexpand '(pushnew 2 l))		; => (let ((x 2)) (if (memql x l) l (setq l (cons x l))))
l					; => (2 1)
(push "a" l)				; => ("a" 2 1)
(pushnew "a" l)				; => ("a" "a" 2 1)
(pushnew "a" l :test 'equal)		; => ("a" "a" 2 1)
(pop l)					; => "a"
l					; => ("a" 2 1)
(macroexpand '(pop l))			; => (car (prog1 l (setq l (cdr l))))
(pop l)					; => "a"
l					; => (2 1)
(pop l)					; => 2
l					; => (1)

P198

Emacs Lispテクニックバイブル

Emacs Lispテクニックバイブル