Data structures 更新深层数据结构(通用Lisp)
我想要一些关于访问和更新多层数据结构的建议。我最初想使用一个通用的引用setf函数,但无法实现。或者,下面列出的函数可以正确地查询和更新数据结构(称为Data structures 更新深层数据结构(通用Lisp),data-structures,common-lisp,Data Structures,Common Lisp,我想要一些关于访问和更新多层数据结构的建议。我最初想使用一个通用的引用setf函数,但无法实现。或者,下面列出的函数可以正确地查询和更新数据结构(称为*kb*)。但它似乎比需要的更复杂。有更简单的解决方案吗 (defun fetch (node edge) "Retrieves all the nodes in the kb associated with a node and edge." (alexandria:hash-table-keys (cdr (assoc edge (ge
*kb*
)。但它似乎比需要的更复杂。有更简单的解决方案吗
(defun fetch (node edge)
"Retrieves all the nodes in the kb associated with a node and edge."
(alexandria:hash-table-keys (cdr (assoc edge (gethash node *kb*)))))
(defun associate (node1 edge node2)
"Adds an association to the kb."
(declare (symbol node1 edge node2))
(when (not (gethash edge *edges*))
(error "Mentioned edge ~A was not predefined" edge))
(let ((alist (gethash node1 *kb*)))
(if alist
(let ((pair (assoc edge alist)))
(if pair
(setf (gethash node2 (cdr pair)) t)
(let ((ht (make-hash-table :test #'eq)))
(setf (gethash node2 ht) t)
(setf (gethash node1 *kb*) (acons edge ht alist)))))
(let ((ht (make-hash-table :test #'eq)))
(setf (gethash node2 ht) t)
(setf (gethash node1 *kb*) (acons edge ht nil))))))
作为背景,
*kb*
数据结构是一个哈希表。哈希表键是符号,值是alist。每个列表由key.value对组成,其中键是符号,值是哈希表。后一个哈希表中的每一个都只是将一组符号表示为具有t值的键。(ps:我选择了alists作为中间结构,因为只有20个左右可能的边缘符号。)感谢您的见解…您可以添加一个可复制的示例吗?您可能会喜欢Access库:您可以添加一个可复制的示例吗?您可能会喜欢Access库: