Search code examples
clips

How can I get the sum of items in a multislot


I have a template like the one shown bellow. How can I get the sum of items in the multislot grades?

(deftemplate student
  (multislot name)
  (multislot grades)
)

Solution

  • You can use the expand$ function. Check in the Basic Programming Guide the Multifield Expansion Function chapter to know more.

    (deftemplate student 
      (multislot name) 
      (multislot grades))  
    
    (defrule grades-sum 
      (student (grades $?grades)) 
      => 
      (printout t "Student grades sum is " (+ (expand$ ?grades)))) 
    
    (assert (student (grades (create$ 1 2 3 4 5)))) 
    (student (name) (grades 1 2 3 4 5))
    
    (run)       
    Student grades sum is 15