列出cLISP中的操作

时间:2016-12-05 13:15:03

标签: list struct lisp

我有以下结构:

(defstruct node
  parent
  state
  cost)

我有node类型的结构列表。

我正在尝试实现两个功能:

(defun findMinimum (list)

returns the node with the smallest cost in the list
)


(defun replaceNode (list)

 Checks if a node with the same state as new-node already exists in the list. 
If so, replaces that node with new-node.
)

我当前的解决方案只是遍历检查每个节点的整个列表的循环。我想知道在cLISP中是否有更有效的方法可以做到这一点?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

我已将函数名称从camelCase更改为更多Lispy样式:

(defun find-minimum (list)
  (reduce #'min list :key #'node-cost) )

对于replace-node我猜你需要另一个带有new-node的参数。让我们假设你想要的是:

(defun replace-node (new-node list)
  (let ((pos-old-node (position (node-state new-node) list :key #'node-state)))
    (when pos-old-node
      (setf (nth pos-old-node list) new-node) )))