next up previous
Next: 5.7 データの置換 Up: 5 リストを変形する操作 Previous: 5.5 要素の削除

5.6 データの挿入

0から数えてn番目の要素になるようにデータを挿入する関数nth-insertや, n番目以降のデータとして展開して挿入する関数nth-insert-listは 以下のようになる.
> (setq a '(1 2 3 4))
(1 2 3 4) 
> (nth-insert 2 'x a)
(1 2 X 3 4) 
> a
(1 2 X 3 4) 
> (nth-insert-list 2 '(a b c) a)
(1 2 A B C X 3 4) 
> a
(1 2 A B C X 3 4)
(defun nth-insert (nth data sexp)
  (let ((x (nthcdr (1- nth) sexp)))
      (rplacd x (cons data (cdr x)))
      sexp))
(defun nth-insert-list (nth data sexp)
  (dolist (x data sexp) (nth-insert nth x sexp)
          (setq nth (1+ nth))))


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