## 我怎样才能在一个定义中定义一个全局变量，并在CLIPS的另一个定义中使用它呢？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (1)
• 关注 (0)
• 查看 (40)

``````(deffunction calculation1
(?x ?y)
(+ ?x ?y))
(defrule rule1
=>
(printout t "What is the first number?")
(assert (number-1 ?number1))
(printout t "What is the second number?")
(assert (number-2 ?number2))
(bind ?theirsum (calculation1 ?number1 ?number2))
(printout t "The sum is " ?theirsum crlf))
(deffacts data
(first num1 ?number1
second num2 ?number2))
``````

### 1 个回答

``````         CLIPS (6.30 3/17/15)
CLIPS> (defrule rule1
=>
(printout t "What is the first number?")
(assert (number-1 ?number1))
(printout t "What is the second number?")
(assert (number-2 ?number2))
(bind ?theirsum (+ ?number1 ?number2))
(printout t "The sum is " ?theirsum crlf)
(build (str-cat
"(deffacts data (first num1 " ?number1 " second num2 " ?number2     "))"
)
)
)
CLIPS> (run)
What is the first number?1
What is the second number?2
The sum is 3
CLIPS> (get-deffacts-list)
(initial-fact data)
CLIPS> (reset)
CLIPS> (facts)
f-0     (initial-fact)
f-1     (first num1 1 second num2 2)
For a total of 2 facts.
``````

``````         CLIPS (6.30 3/17/15)

CLIPS> (defrule rule1
=>
(printout t "What is the first number?")
(assert (number-1 ?number1))
(printout t "What is the second number?")
(assert (number-2 ?number2))
(bind ?theirsum (+ ?number1 ?number2))
(printout t "The sum is " ?theirsum crlf)
(printout data
"(deffacts data (first num1 " ?number1 " second num2 " ?number2 "))"
crlf)
(close data)
)
CLIPS> (run)
What is the first number?1
What is the second number?2
The sum is 3