next up previous
Next: 3.5 置換した形のコピーを作る subst Up: 3 リスト構造の変更 Previous: 3.3 nconc

3.4 delete

リストから要素を取り除くものにremoveがあったが, リストの構造を変えてしまう手続きとしてdeleteがある.
(setq x '(1 2 3 4 3 2))
(1 2 3 4 3 2)
(remove 3 x)
(1 2 4 2)
x
(1 2 3 4 3 2)
(remove 2 x)
(1 3 4 3)
x
(1 2 3 4 3 2)
(delete 2 x)
(1 3 4 3)
x
(1 3 4 3)
(delete 3 x)
(1 4)
x
(1 4)
ここで,任意個の引数をとるappend, nconc の定義を示す.
(defun append* (&rest args)
  (cond  ((null args) nil)
         (t (apply 
             #'append2
               (car args)
               (apply #'append (cdr args))))))
(defun append2 (a b)
  (if  (null a) b
       (cons (car  a) (append2 (cdr a) b))))

(defun nconc* (a &rest args)
  (cond
    ((null args) a)
    (t (apply
         #'nconc*
         (cons
           (nconc2 a (car args))
           (cdr args))))))
(defun nconc2 (a b)
  (rplacd (last a) b) a)

(defun last (x)
  (if (null (cdr x)) x (last (cdr x))))


generated through LaTeX2HTML. M.Inaba 平成18年5月7日