使用標準格式。見http://www.gigamonkeys.com/book/syntax-and-semantics.html#formatting-lisp-code。
(defun update-feature-list (feature)
(let ((feature-index))
(setq feature-index (position feature *features-list*))
(cond ((equal feature-index nil)
;;case 1 If feature index ==nil ;;we need to add the feature
(push feature (cdr (last *features-list*)))
(setq feature-index (position feature *features-list*))))))
A cond
一個條款是沒有意義的。我想你的意思是when
,而你 想在任何情況下更新索引,我。即條件之外。
(defun update-feature-list (feature)
(let ((feature-index))
(setq feature-index (position feature *features-list*))
(when (equal feature-index nil)
(push feature (cdr (last *features-list*))))
(setq feature-index (position feature *features-list*))))
你並不需要設置一個局部變量只是回到它:
(defun update-feature-list (feature)
(let ((feature-index))
(setq feature-index (position feature *features-list*))
(when (equal feature-index nil)
(push feature (cdr (last *features-list*))))
(position feature *features-list*)))
您可以創建直接在let
頭綁定:
(defun update-feature-list (feature)
(let ((feature-index (position feature *features-list*)))
(when (equal feature-index nil)
(push feature (cdr (last *features-list*))))
(position feature *features-list*)))
而不是檢查equal
... nil
,使用null
:
(defun update-feature-list (feature)
(let ((feature-index (position feature *features-list*)))
(when (null feature-index)
(push feature (cdr (last *features-list*))))
(position feature *features-list*)))
可以內聯變量:
(defun update-feature-list (feature)
(when (null (position feature *features-list*))
(push feature (cdr (last *features-list*))))
(position feature *features-list*))
而不是null
... position
,使用not
... member
:
(defun update-feature-list (feature)
(when (not (member feature *features-list*))
(push feature (cdr (last *features-list*))))
(position feature *features-list*))
在列表的最後一個利弊的cdr
是不是一個地方,你會想推 東西。我想你想append
,但在大多數情況下,你應該 而不是推到列表的前端,這是更有效的。 對此也有pushnew
。回到新的位置不 多大意義了,但是:
(defun update-feature-list (feature)
(pushnew feature *features-list*)
(position feature *features-list*))
如果你真的需要這種順序和位置,可以使用可調節的載體來代替:
(defvar *features-list* (make-array 10
:adjustable t
:fill-pointer 0))
(defun add-feature (feature)
(or (position feature *features-list*)
(vector-push-extend feature *features-list*))) ; v-p-e returns the index
請修復您的代碼 - 縮進和括號。現在是不可讀的。 – sds
是的..關於什麼可能導致錯誤的任何想法? – ijuio
請參閱[此評論](http://stackoverflow.com/questions/34793011/push-doesnt-modify-the-list-being-a-function-argument#comment57330915_34793011)。 –