LISP - 全局变量在重新初始化后保持原有值 [英] LISP - Global variable keep their old value after reinitialization
问题描述
有一个全局变量: BF - > fact base。
我这样初始化:
(defvar * BF * NIL)
我的主函数调用函数initialize,它用大数据设置全局变量。
(defun initialize()
(setf * BF *
'(
(metric (
(CPU使用率为零)
(RPI NIL)
(ART NIL)
(conercant-invocation NIL)
(摊位数NIL)
(GC字节在用的NIL)
(可用线程无)
(可用连接无)
(实例数无)
))
(问题(
(livelock T)
(内存泄漏T)
(代码无效T)
(过载T)
(分配不足T)
(内部阻塞点T)
(线程泄露T)
(阻塞 - 死锁T)
(无止境重试T)
(过度使用外部系统T)
(pig-in-a-python T)
(too-many-layers T)
(后端瓶颈T)
(频繁GC资源泄漏(非阻塞 - 死锁T)
(慢速后端T)
(突然慢后端T)
(线程泄露T)
) )
(
(突然T)
(一致T)
(定期T)
(累进T)
)
)
)
)
在第一次使用这个函数时,当我打印 BF ,没关系。
然后我调用一个函数来修改 BF :
(defun apply-rule(类型名称值)
pre>
;获得这条规则的结论列表
(let((结论(得到结论名称值)))
(如果(null结论)
(if不是(等于3价值))
(从appliquer-regle返回NIL)
(从appliquer-regle返回)
)
)
;迭代在* BF *
(dolist(e(cadr(assoc'problem * BF *)))
的所有问题中,如果问题不在结论列表中,则将其值设为false
(如果(和(成员(车e)结论)(等于(cadr e)'T))
()
(setf(cadr e)无)
)
)
(返回应用规则'T)
)
(返回应用规则无)
)
这个函数可以工作。
但是当我想再次使用函数initialize时,它不起作用。当我打印 BF 时,它包含旧值...
如何重新初始化我的全局变量?
对不起,我是英文的,我是法国人。
您正在修改文字数据。最好避免它,因为它的效果在便携式代码中是未定义的。想象一下,通过共享文字列表来节省空间的编译器。
确保您的变量
* BF *
已新分配数据。每次初始化变量时,使用 COPY-TREE 复制列表。I am creating a expert system with Common Lisp for my study. There is a global variable : BF -> fact base.
I initialize like that :
(defvar *BF* NIL)
My "main function" call to the function "initialize" which set the global variable with big data.
(defun initialize () (setf *BF* '( (metric ( (CPU-utilization NIL) (RPI NIL) (ART NIL) (concurent-invocation NIL) (stall-count NIL) (GC-bytes-in-use NIL) (available-thread NIL) (available-connection NIL) (instance-count NIL) )) (problem ( (livelock T) (memory-leaks T) (code-inefficient T) (overload T) (under-allocation T) (internal-chokepoint T) (thread-leaks T) (blocking-deadlock T) (unending-retries T) (overuse-external-system T) (pig-in-a-python T) (too-many-layers T) (backend-bottleneck T) (frequent-GC-resource-leaks T) (slow-backend T) (suddenly-slow-backend T) (nonblocking-deadlock T) (thread-leaks T) )) (category ( (sudden T) (consistent T) (periodic T) (progressive T) )) ) ) )
In the first use of this function, when i print BF, it's ok. Then I call a function wich modify BF :
(defun apply-rule (type name value) ; Get conclusion list for this rule (let ((conclusion (get-conclusion name value))) (if (null conclusion) (if (not (equal 3 value)) (return-from appliquer-regle NIL) (return-from appliquer-regle 'T) ) ) ; Iterate on all problems in *BF* (dolist (e (cadr (assoc 'problem *BF*))) ; If the problem is not in conclusion list, set it value to false (if (and (member (car e) conclusion) (equal (cadr e) 'T)) () (setf (cadr e) NIL) ) ) (return-from apply-rule 'T) ) (return-from apply-rule NIL) )
This function work. But when I want to use again the function "initialize", it doesn't work. When I print BF, it contain the old values ... How can I do to reinitialize my global variable ?
Sorry for my english, I'm french ^
解决方案You are modifying literal data. It's best to avoid it, since its effects are undefined in portable code. Imagine a compiler which saves space by sharing literal lists.
Make sure that your variable
*BF*
has freshly allocated data. Use COPY-TREE to copy the list every time you initialize the variable.这篇关于LISP - 全局变量在重新初始化后保持原有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!