如何比较CLIPS中2个多字段变量之间的符号

时间:2018-03-13 22:07:29

标签: clips

问题是比较SYMBOL类型的两个多字段变量。 这是我尝试开发的代码示例。

CLIPS>(defrule r       
=>
(printout t "Input A: ")
(bind $?A (explode$ (readline)))

(printout t "Input B: ")
(bind $?B (explode$ (readline)))

(if (member$ $?A $?B) then (printout t " Something ..." crlf)))
CLIPS> (run)
Input A: 1 2 3 4 5
Input B: 7 3 2 1 6
CLIPS> 

我想将$?A的每个参数(或值)与$?B的每个参数进行比较,如果两者中至少有一个参数在$?A或$?B中,则if测试变为TRUE。 / p>

1 个答案:

答案 0 :(得分:1)

您可以编写一个函数来测试两个多字段值的交集:

CLIPS> 
(deffunction intersectionp (?m1 ?m2)
   (foreach ?i1 ?m1
      (foreach ?i2 ?m2
         (if (eq ?i1 ?i2)
            then (return TRUE))))
   (return FALSE))
CLIPS> 
(defrule r       
   =>
   (printout t "Input A: ")
   (bind ?A (explode$ (readline)))

   (printout t "Input B: ")
   (bind ?B (explode$ (readline)))

   (if (intersectionp ?A ?B) then (printout t " Something ..." crlf)))
CLIPS> (run)
Input A: 1 2 3 4 5
Input B: 7 3 2 1 6
 Something ...
CLIPS> (reset)
CLIPS> (run)
Input A: 1 2 3
Input B: 4 5 6
CLIPS> 

或者,您可以使用模式匹配来测试交叉点:

CLIPS> (clear)
CLIPS>    
(defrule r       
   =>
   (printout t "Input A: ")
   (bind ?A (explode$ (readline)))
   (assert (A ?A))

   (printout t "Input B: ")
   (bind ?B (explode$ (readline)))
   (assert (B ?B)))
CLIPS> 
(defrule intersect
   (exists (A $? ?v $?)
           (B $? ?v $?))
   =>
   (printout t " Something ..." crlf))
CLIPS> (reset)
CLIPS> (run)
Input A: 1 2 3 4 5
Input B: 7 3 2 1 6
 Something ...
CLIPS> (reset)
CLIPS> (run)
Input A: 1 2 3
Input B: 4 5 6
CLIPS>