1

私は Lisp の分野の初心者です... bfs で 8 パズルを解くコードを書いています...

訪問したリストをグローバルリストに保存し、その値を関数内から定期的に変更したい...

(defparameter *vlist* nil)
(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))))))
                    )
                )
            )

defparameter 1 は私のグローバル変数です...そして setq でその値を変更したい関数を使用しています... defvar、setf、set、および可能なすべての組み合わせも使用しました.....誰か助けてくれますか?? ??

4

3 に答える 3

2

コードは次のとおりです (標準の Lisp スタイル用に再フォーマットされています)。

(defparameter *vlist* nil)
(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)))))))))

1 つの明確な問題としてのコード。

フォーム((setq *vlist* ...) (bfs-core ...))は関数呼び出しになります。(exp1 exp2 exp3)thenのようなフォームがある場合、とexp1のパラメーター値に関数が適用されます。あなたのは関数に評価されないものであり、もちろん、それを望んでいませんでした。exp2exp3exp1(setq *vlist* ...)

少なくとも間違った関数呼び出しを削除するコードの書き直されたバージョンは次のとおりです。

(defparameter *vlist* nil)
(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
              (progn
                (setq *vlist* (append cur-node *vlist*))
                (bfs-core (append tmp-node-list (expand cur-node)))))))))
于 2013-09-29T16:10:39.187 に答える