你當然可以從內部功能改變全局變量:
[1]> (defparameter *visited-lists* nil)
*VISITED-LISTS*
[2]> *visited-lists*
NIL
[3]> (defun change-global-value()
(setf *visited-lists* (append (list 'new-value)
*visited-lists*)))
CHANGE-GLOBAL-VALUE
[4]> *visited-lists*
NIL
[5]> (change-global-value)
(NEW-VALUE)
[6]> *visited-lists*
(NEW-VALUE)
[7]> (change-global-value)
(NEW-VALUE NEW-VALUE)
[8]> *visited-lists*
(NEW-VALUE NEW-VALUE)
但是,讓我們看看你的代碼一些:
(defun bfs-core(node-list)
(let (cur-node tmp-node-list)
(if (null node-list)
NIL
(progn
; (if (= 1 (length node-list))
(setq cur-node (car node-list))
(setq tmp-node-list (cdr node-list))
(if (goalp cur-node)
cur-node
((setq *vlist* (append cur-node *vlist*))
(bfs-core (append tmp-node-list (expand cur-node))))))
)
)
)
首先,讓我們在正確的行結束括號,並去除註釋掉的代碼。大多數Lisp的程序員不收他們的括號像括號中的ALGOL風格的語言:
(defun bfs-core(node-list)
(let (cur-node tmp-node-list)
(if (null node-list)
NIL
(progn
(setq cur-node (car node-list))
(setq tmp-node-list (cdr node-list))
(if (goalp cur-node)
cur-node
((setq *vlist* (append cur-node *vlist*))
(bfs-core (append tmp-node-list (expand cur-node)))))))))
現在,我們有一個nil
爲if
的第一家分店。我們可以將其更改爲unless
,它有一個內置的progn
,所以我們並不需要,要麼:
(defun bfs-core(node-list)
(let (cur-node tmp-node-list)
(unless (null node-list) ;;changed this line to an unless, dropped nil, progn
(setq cur-node (car node-list))
(setq tmp-node-list (cdr node-list))
(if (goalp cur-node)
cur-node
((setq *vlist* (append cur-node *vlist*))
(bfs-core (append tmp-node-list (expand cur-node))))))))
我們還使用let
一些變量設置爲nil
,當我們進去unless
,我們立即將變量設置爲我們實際想要使用的值。讓我們打開它,這樣我們只如果我們打算使用它們創建變量:
(defun bfs-core(node-list)
(unless (null node-list) ;;switched this line and the let below
(let ((cur-node (car node-list)) ;;also set the variables inside the let
(tmp-node-list) (cdr node-list))
(if (goalp cur-node)
cur-node
((setq *vlist* (append cur-node *vlist*))
(bfs-core (append tmp-node-list (expand cur-node))))))))
好了,我們已經在更清潔的代碼。好極了!
讓我們看看這裏的呼叫中的一個:
((setq *vlist* (append cur-node *vlist*))
(bfs-core (append tmp-node-list (expand cur-node))))
您的意思是把這個作爲一個單調用,而不是兩個?這與此有區別:
((setq *vlist* (append cur-node *vlist*)))
(bfs-core (append tmp-node-list (expand cur-node)))
您是否看到區別?第一個是單一的陳述;第二個是兩個。您可能需要第二個,因爲您要更改*vlist*
,然後致電bfs-core
。而且,這樣做是,你需要progn
:
(defun bfs-core(node-list)
(unless (null node-list)
(let ((cur-node (car node-list))
(tmp-node-list) (cdr node-list))
(if (goalp cur-node)
cur-node
(progn (setq *vlist* (append cur-node *vlist*))
(bfs-core (append tmp-node-list (expand cur-node))))))))
您的代碼已經包括分配到'* VLIST *'使用'setq'。它是'(setq * vlist *(append cur-node * vlist *)'。不清楚你在問什麼,但是你的語法不對。'if'應該看起來像'(if(goalp cur-node )cur-node(progn(setq ...)(bfs-core ...)))'。 –