我正在嘗試做一個列表作爲家庭作業的一部分。如何在lisp中創建對列表?
我想這樣做(在函數的中間位置)
(setq list1 (append list1 (cons n1 n2)))
出於某種原因,我不明白,這正常工作與第一對,但我嘗試添加第二對,這個錯誤彈出:
*** - APPEND: A proper list must not end with 2
我該如何解決這個問題?
因此,繼續關於這個問題,由於給出的答案,我能夠糾正我的問題。但是一個新的問題出現了,我認爲它與它有關。所以,我有這個功能:
(defun action(state)
(let ((list_actions '())
(limNumActions (1- (list-length state)))
(limNumSubActions 0)
(numActions 0)
(numSubActions 0))
(loop for numActions from 0 to limNumActions do
(setq limNumSubActions (1- (list-length (nth numActions state))))
(loop for numSubActions from 0 to limNumSubActions do
(setq list_actions (append list_actions
(list (cons numActions numSubActions))))
(print 'list_actions)
(print list_actions)))))
我用print
函數作爲一個簡單的「調試器」。它返回這個:
LIST_ACTIONS
((0 . 0))
LIST_ACTIONS
((0 . 0) (0 . 1))
LIST_ACTIONS
((0 . 0) (0 . 1) (1 . 0))
LIST_ACTIONS
((0 . 0) (0 . 1) (1 . 0) (1 . 1))
NIL
而這正是我期待的結果!除了NIL
部分...您能否理解爲什麼列表list_actions
最後是NIL
?
僅供參考:請勿添加作業標籤。它已經過時並正在被刪除。 – Bart
感謝您的信息 – FriedRike
所以,也許我應該在第一個循環結束時做類似'(RETURN list_actions)'的事情? 我真的試過了,發生了一個錯誤: '*** - RETURN-FROM:沒有名爲NIL的塊當前可見「 – FriedRike