Search code examples
clips

CLIPS: counting different products


I have some facts in the form of a list: (list day1 apple milk bread) (list day 2 bread milk) and I would like to count the different products in that facts for each day. How can I count them? The result that I want is (list day1 3) (list day2 2)


Solution

  •          CLIPS (6.4 2/9/21)
    CLIPS> 
    (defrule list-count
       (list ?day $?items)
       =>
       (assert (count ?day (length$ ?items))))
    CLIPS> 
    (assert (list day1 apple milk bread)
            (list day2 bread milk))
    <Fact-2>
    CLIPS> (agenda)
    0      list-count: f-2
    0      list-count: f-1
    For a total of 2 activations.
    CLIPS> (run)
    CLIPS> (facts)
    f-1     (list day1 apple milk bread)
    f-2     (list day2 bread milk)
    f-3     (count day2 2)
    f-4     (count day1 3)
    For a total of 4 facts.
    CLIPS>