2010-12-08 112 views
5

兩個問題:在common-lisp中,如何將元素插入到列表中?

1.I想要的功能插入就地列表中的元素(在任意位置,但在列表的開始,見問題2的原因),使得:

CL> (defun insert-in-place (the-list after-position new-element) ....) => ... 
CL> (setf testy-list (list 'a 'b 'c 'd)) => ... 
CL> testy-list => ('A 'B 'C 'D) 
CL> (insert-in-place testy-list 1 'BOOOO) => ... 
CL> testy-list => ('A 'B 'BOOOO 'C 'D) 

2.我認爲通過函數在列表的開始處插入一個元素是不可能的,因爲這些參數是按值傳遞的,所以由於列表的第一個cons單元格被傳遞,所以它是通過值傳遞的它是一個副本,所以改變它的車只會改變一輛複製車,而不是原來的車,儘管下面的cons單元是共享的,並且可以在原地進行改變。我對麼?

回答

8

1)在這裏它是:

(defun insert-after (lst index newelt) 
    (push newelt (cdr (nthcdr index lst))) 
    lst) 
(insert-after '(a c d) 0 'b) => (A B C D) 

2)利弊細胞的破壞性變形例:

(setf testy-list '(a bar)) 
(defun modify (list) 
    (setf (first list) 'foo)) 
(modify testy-list) 
testy-list => (FOO BAR) 

這第一cons單元的轎廂設置爲「富。

+0

謝謝。關於問題2:當你打電話(修改測試列表)什麼是正在傳遞修改?測試列表的第一個cons單元是值還是參考?我的意思是你回答說我是不正確的,但我無法找到我關於問題2的論點中的缺陷...... – Paralife 2010-12-08 14:22:18

0

我爲我的一個項目做了這個,處理索引0,如果索引大於列表的長度,新項目被附加到列表的末尾。請注意,它會創建一個新列表,因此它可能不適用於您。我包括它希望對某人有用。

(defun list-insert-at (lst index new-value) 
    (let ((retval nil)) 
    (loop for i from 0 to (- (length lst) 1) do 
     (when (= i index) 
     (push new-value retval)) 
     (push (nth i lst) retval)) 
    (when (>= index (length lst)) 
     (push new-value retval)) 
    (nreverse retval))) 

CL-USER> test 
(1 2 3 4 5) 
CL-USER> (list-insert-at test 5 'a) 
(1 2 3 4 5 A) 
CL-USER> (list-insert-at test 0 'a) 
(A 1 2 3 4 5)