Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lisp 为什么setq会删除我的列表_Lisp_Common Lisp - Fatal编程技术网

Lisp 为什么setq会删除我的列表

Lisp 为什么setq会删除我的列表,lisp,common-lisp,Lisp,Common Lisp,正如标题所说,我试图将项目附加到名为solution的列表中,下面是代码: (defun add-solution (n) (let ((solution)) (do((current-node '() (next-state current-node n nil))) ((equal current-node '(0 0 0 0)) solution) (if (goal-test current-node n)

正如标题所说,我试图将项目附加到名为solution的列表中,下面是代码:

(defun add-solution (n)
    (let ((solution)) 
        (do((current-node '() (next-state current-node n nil)))
            ((equal current-node '(0 0 0 0)) solution)
            (if (goal-test current-node n)
                (progn
                    (format t "Cur: ~S~%" current-node)
                    (setq solution (append solution (list current-node)))
                    (format t "Solution: ~S~%" solution)
                )
            )   
        )
    )
)
每次新的当前节点是一个类似于:
(1 7 8 14)、(2 4 11 13),
但当循环返回时,
返回((1)(2))…
我需要的是
((1 7 8 14)(2 4 11 13))
。不确定那里发生了什么

编辑: 我在setq前后添加了format函数,输出如下:


Cur: (1 7 8 14)
Solution: ((1 7 8 14))
Cur: (2 4 11 13)
Solution: ((1)(2 4 11 13))

整个过程完成后,返回值再次变成
((1)(2))
。我并没有真正做任何修改
解决方案的事情。

看起来您的错误在其他地方

风格:

我会这样编写/格式化代码:

(defun add-solution (n)
  (do ((solution nil)
       (current-node '() (next-state current-node n nil)))
      ((equal current-node '(0 0 0 0)) solution)
    (when (goal-test current-node n)
      (setq solution (append solution (list current-node))))))

请注意,这是错误的代码,因为您在循环中将一项附加到列表的末尾。这是一个潜在的非常昂贵的操作。Lisp列表被优化为添加到前面,而不是末尾。

代码中似乎没有任何错误(除了样式问题,例如缩进)。因此,问题可能出在
下一状态
函数中-可能是以破坏性方式修改其第一个参数(
当前节点
),从而导致已收集的节点无效?是的。解决了我的问题。。非常感谢!这让我发疯了:/感谢样式建议。