Rui Loureiro
Rui Loureiro

Reputation: 119

List operations in cLISP

I have the following struct:

(defstruct node
  parent
  state
  cost)

I have a list of structs of type node.

I am trying to implement two functions:

(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.
)

My current solution is just a loop through the entire list that checks every node. I wonder if there is a more efficient way to do this in cLISP?

Any help will be greatly appreciated.

Upvotes: 0

Views: 66

Answers (1)

Leo
Leo

Reputation: 1934

I have changed the name of your functions from camelCase to more Lispy style:

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

For replace-node I guess you need another argument with the new-node. Let's assume that's what you want:

(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) )))

Upvotes: 2

Related Questions