如何从CLIPS中的列表中找到最大元素?

我正在尝试从列表中找出最大的元素,例如

(deffacts list 
   (list 1 2 3 4 5 6 7 6 5 4 3 2 1))
在CLIPS中

。我该如何以一种非常简单的方式去屑处理?

此外,如果我有一个患者模板,则具有以下位置:

(deftemplate patient
   (slot name)
   (slot age)
   (multislot tens_max)
   (multislot tens_min))

(deffacts data_patient
    (patient (name John) (age 22) (tens_max 13 15 22 11) (tens_min 6 7 14 6))
)

我想从最后一个多槽tens_min中找出最大的元素,我该怎么做?

任何建议,我将不胜感激。

yuelao0308 回答:如何从CLIPS中的列表中找到最大元素?

您可以使用 max 函数查找其参数的最大值。您可以在规则条件下将数字列表绑定到多字段变量。但是 max 函数需要单独的参数,因此您不能仅将其传递为多字段值。您可以使用 expand $ 函数将多字段值拆分为用于函数调用的单独参数。 max 函数在CLIPS 6.3中期望至少2个参数,在CLIPS 6.4中期望至少1个参数,因此,为了完整起见,您需要处理这些情况。您可以创建一个deffunction来处理代码中的这些极端情况。

         CLIPS (6.31 6/12/19)
CLIPS> 
(deffunction my-max ($?values)
   (switch (length$ ?values)
      (case 0 then (return))
      (case 1 then (return (nth$ 1 ?values)))
      (default (return (max (expand$ ?values))))))
CLIPS> 
(deffacts list 
   (list 1 2 3 4 5 6 7 6 5 4 3 2 1))
CLIPS> 
(defrule list-max
   (list $?values)
   =>
   (printout t "list max = " (my-max ?values) crlf))
CLIPS> 
(deftemplate patient
   (slot name)
   (slot age)
   (multislot tens_max)
   (multislot tens_min))
CLIPS> 
(deffacts data_patient
    (patient (name John) (age 22) (tens_max 13 15 22 11) (tens_min 6 7 14 6)))
CLIPS> 
(defrule patient-max
   (patient (tens_min $?values))
   =>
   (printout t "patient max = " (my-max ?values) crlf))
CLIPS> (reset)
CLIPS> (run)
patient max = 14
list max = 7
CLIPS> 
本文链接:https://www.f2er.com/3108972.html

大家都在问