Pergunta

Having

(deffacts people
(customer (name a) (money 20) )
(customer (name b) (money 30) )
(customer (name c) (money 40) )
)

I want to calculate the average money. How can I count how many customers I have and then, the average?

Foi útil?

Solução

CLIPS> (deftemplate customer (slot name) (slot money))
CLIPS> 
(deffacts people
   (customer (name a) (money 20))
   (customer (name b) (money 30))
   (customer (name c) (money 40)))
CLIPS> (reset)
CLIPS> 
(defrule average
   (exists (customer))
   =>
   (bind ?count 0)
   (bind ?sum 0)
   (do-for-all-facts ((?f customer)) TRUE
      (bind ?count (+ ?count 1))
      (bind ?sum (+ ?sum ?f:money)))
   (printout t "There are " ?count " customers with average money of " (/ ?sum ?count) crlf))
CLIPS> (reset)
CLIPS> (run)
There are 3 customers with average money of 30.0
CLIPS>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top