我知道 CLIPS 中的
member$
功能。但它的缺点是它只适用于作为输入的文字值。 CLIPS 是否具有对谓词测试元素的所有/任何/无函数?可能看起来像这样:(for-all <range-var> <source> <predicate>)
。
我面临的问题:
(deftemplate list (multislot vals))
(defrule first-high-val
(list
(vals
$?b ;; all of these should be less than or equal to 10
?x ;; first value to be greater than 10
$?e
) )
(test (for-all ?k $?b (<= ?k 10)))
=>
(println ?x)
)
简单地做:
(defrule first-high-val
(list
(vals
$?b
?x & :(> ?x 10)
$?e
) )
=>
(println ?x)
)
不正确,因为对于
(4 2 7 4 33 5 9 3 11 8 3 0)
,规则将分别激活 33
和 11
,而 11
不是大于 10 的第一个数。
CLIPS> (deftemplate list (multislot vals))
CLIPS>
(deffunction for-every (?list ?op ?value)
(foreach ?item ?list
(if (not (funcall ?op ?item ?value)) then (return FALSE)))
(return TRUE))
CLIPS>
(defrule first-high-val
(list (vals $?b ?x&:(> ?x 10) $?e))
(test (for-every ?b <= 10))
=>
(println ?x))
CLIPS> (assert (list (vals 4 2 7 4 33 5 9 3 11 8 3 0)))
<Fact-1>
CLIPS> (run)
33
CLIPS>