PhoenixQoH
PhoenixQoH

Reputation: 67

How to know when a list is ordered in CLIPS

I am making a CLIPS exercise where I order a list in descending order. The thing is that I want to print the list only one time and only once it has been completely ordered, does anybody know how can I check that? So far I have this, and it properly orders the list. Thanks in advance :)

(deffacts hechos-iniciales
(lista 5 7 3 1 8 4 2 6)
)

(defrule ordena-lista
?indice<-(lista $?ini ?num1 ?num2 $?fin)
(test (< ?num1 ?num2))
=>
(assert (lista $?ini ?num2 ?num1 $?fin))
(retract ?indice)
)

Upvotes: 0

Views: 818

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

You can either create a lower salience rule to print the result or have the rule printing the list check that there are no unsorted lists.

CLIPS> 
(deffacts hechos-iniciales
   (lista 5 7 3 1 8 4 2 6))
CLIPS> 
(defrule ordena-lista
   ?indice<-(lista $?ini ?num1 ?num2 $?fin)
   (test (< ?num1 ?num2))
   =>
   (assert (lista $?ini ?num2 ?num1 $?fin))
   (retract ?indice))
CLIPS> 
(defrule print-lista-1
   (declare (salience -10))
   (lista $?values)
   =>
   (printout t "print1: " (str-implode ?values) crlf))
CLIPS> 
(defrule print-lista-2
   (lista $?values)
   (not (lista $?ini ?num1 ?num2&:(< ?num1 ?num2) $?fin))
   =>
   (printout t "print2: " (str-implode ?values) crlf))
CLIPS> (reset)
CLIPS> (run)
print2: 8 7 6 5 4 3 2 1
print1: 8 7 6 5 4 3 2 1
CLIPS>

Upvotes: 1

Related Questions