Comparing each element in a multislot of 2 facts

127 views
Skip to first unread message

iancu denis

unread,
Jan 15, 2020, 2:02:06 PM1/15/20
to CLIPSESG
Hello, I am trying to learn to use Clips and would like to know how to do something like comparing each element from a multislot in a fact, to each element from a multislot in another fact. For example the following example
(deftemplate set
   
(slot name)
   
(multislot members)
)

(deffacts sets
   
(set (name set1) (members 1 2 3 5 7 12))
   
(set (name set2) (members 2 4 3 1))
)

How can I compare each member of the "set1" with each member of "set2" and assert a new fact when the elements are equal.

CLIPS Support

unread,
Jan 15, 2020, 2:12:23 PM1/15/20
to CLIPSESG
Here's how you can check for common members. In this example a message is printed, but you can change that to assert a fact instead.

         CLIPS (6.31 6/12/19)
CLIPS> 
(deftemplate set
    (slot name)
    (multislot members)
)
CLIPS> 
(deffacts sets
    (set (name set1) (members 1 2 3 5 7 12))
    (set (name set2) (members 2 4 3 1))
)
CLIPS> 
(defrule common
   (set (name set1) (members $? ?m $?))
   (set (name set2) (members $? ?m $?))
   =>
   (printout t "Member " ?m " is in both set1 and set2" crlf))
CLIPS> (reset)
CLIPS> (run)
Member 2 is in both set1 and set2
Member 3 is in both set1 and set2
Member 1 is in both set1 and set2
CLIPS> 
Reply all
Reply to author
Forward
0 new messages