我似乎在这里有一个盒子里的问题 在给定键和散列表的情况下,以下代码应返回与键对应的值,如果映射中不存在该键,则返回错误:
(defun get-graph-node (key graph)
(let ((result (gethash key graph)))
(if (nth-value 1 result)
(nth-value 0 result)
(error "no node matches the key"))))
在大多数情况下它都有,但我有这种奇怪的情况:跑步:
(gethash 0 *g*)
返回
#S(GRAPH-NODE$
:DATA "("$
:EDGES (#S(GRAPH-NODE :DATA "b" :EDGES NIL)$
#S(GRAPH-NODE :DATA "a" :EDGES NIL)))
T
但是
(get-graph-node 0 *g*)
表示get-graph-node
中定义的错误检查* g *给了我这个:
Count: 5
Size: 16
Test: EQL
Rehash size: 1.5
Rehash threshold: 1.0
[clear hashtable]
Contents:
0 = #S(GRAPH-NODE :DATA "(" :EDGES (#S(GRAPH-NODE :DATA "b" :EDGES NIL) #S(GRAPH-NODE :DATA "a" :EDGES NIL)))
[remove entry]
1 = #S(GRAPH-NODE :DATA "a" :EDGES NIL) [remove entry]
2 = #S(GRAPH-NODE :DATA "|" :EDGES (#S(GRAPH-NODE :DATA ")" :EDGES (NIL)))) [remove entry]
3 = #S(GRAPH-NODE :DATA "b" :EDGES NIL) [remove entry]
4 = #S(GRAPH-NODE :DATA ")" :EDGES (NIL)) [remove entry]
所以键0应该在地图中? 对我可以告诉我自己失踪的绅士来说,这是我的一顶帽子。
答案 0 :(得分:6)
将gethash的结果赋给变量result时,只存储函数返回的多个值中的第一个。要存储它们,你应该这样做:
(defun get-graph-node (key graph)
(multiple-value-bind (result exists)
(gethash key graph)
(if exists
result
(error "no node matches the key"))))