gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:07:13 26 4
gpt4 key购买 nike

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

(deffacts list 
(list 1 2 3 4 5 6 7 6 5 4 3 2 1))

在剪辑中。我怎样才能做到这一点,以一种非常简单的方式,在 derule 中?

另外,如果我有一个病人模板,带有以下插槽:
(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,我该怎么做?

我会很感激任何建议。

最佳答案

您可以使用 最大 函数找到其参数的最大值。您可以将数字列表绑定(bind)到规则条件中的多字段变量。 最大 然而,函数需要单独的参数,所以你不能只传递一个多字段值。您可以使用 展开$ 函数将多字段值拆分为函数调用的单独参数。 最大 函数在 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>

关于list - 如何从 CLIPS 的列表中找到最大元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58840031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com