具有带有值的列表.如果项目存在多次,则添加值



我想开始使用 CLIPS,尝试实现一个非常简单的任务:

Item1 = 5
Item2 = 7
Item3 = 8
Item1 = 10

假设我有上面的变量或赋值。现在,我想要一个脚本来对具有最小值到最大值的项目进行排序。此外,具有相同名称的项目(项目 1(应汇总其值。

已经在其他语言中意识到了这一点,但我完全迷失在 CLIPS 中。

NewList= {Item2=7,Item3=8,Item1=15}
CLIPS> 
(defclass ENTRY
   (is-a USER)
   (slot key)
   (slot value))
CLIPS>    
(defclass DICTIONARY
   (is-a USER)
   (multislot entries))
CLIPS> 
(defmessage-handler DICTIONARY find-key-position (?key)
   (bind ?index 1)
   (foreach ?e ?self:entries
      (if (eq (send ?e get-key) ?key)
         then
         (return ?index))
      (bind ?index (+ ?index 1)))
   (return FALSE))
CLIPS> 
(defmessage-handler DICTIONARY add (?key ?value)
   (bind ?position 
      (send ?self find-key-position ?key))
   (if ?position
      then
      (bind ?entry (nth$ ?position ?self:entries))
      (send ?entry put-value (+ (send ?entry get-value) ?value))
      else
      (bind ?position (+ (length$ ?self:entries) 1))
      (bind ?entry
         (make-instance of ENTRY (key ?key) (value ?value)))
      (slot-direct-insert$ entries ?position ?entry)))
CLIPS>    
(defmessage-handler DICTIONARY display ()
   (printout t "{")
   (foreach ?e ?self:entries
      (printout t " " (send ?e get-key) "=" (send ?e get-value)))
   (printout t " }" crlf))
CLIPS>    
(deffunction sort-by-value (?e1 ?e2)
   (> (send ?e1 get-value) (send ?e2 get-value)))
CLIPS>    
(defmessage-handler DICTIONARY sort ()
   (bind ?self:entries (sort sort-by-value ?self:entries))
   TRUE)
CLIPS> (make-instance d1 of DICTIONARY)
[d1]
CLIPS> (send [d1] display)
{ }
CLIPS> (send [d1] add item1 5)
TRUE
CLIPS> (send [d1] add item2 7)
TRUE
CLIPS> (send [d1] add item3 8)
TRUE
CLIPS> (send [d1] display)
{ item1=5 item2=7 item3=8 }
CLIPS> (send [d1] add item1 10)
15
CLIPS> (send [d1] display)
{ item1=15 item2=7 item3=8 }
CLIPS> (send [d1] sort)
TRUE
CLIPS> (send [d1] display)
{ item2=7 item3=8 item1=15 }
CLIPS> 

相关内容

  • 没有找到相关文章

最新更新