LISP - Global variable keep their old value after

2019-01-29 02:57发布

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 ^

1条回答
神经病院院长
2楼-- · 2019-01-29 03:25

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.

查看更多
登录 后发表回答