Chocksmith
Chocksmith

Reputation: 1208

List manipulation in CLIPS (expert system)

I just want a function to print on item per line. I am trying:

(deffunction myprint (?first $?rest)
(if (neq ?rest nil) then
    (printout t ?first crlf)
    (myprint ?rest)))

What is wrong?

Upvotes: 0

Views: 1979

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

Use the length function to determine if a list is empty (a return value of 0). Comparing a list to the symbol nil will always fail.

You want to print ?first even if ?rest is empty. Otherwise the last element will never be printed.

It is not necessary to use recursion.

CLIPS> 
(deffunction myprint ($?rest)
   (foreach ?r $?rest
      (printout t ?r crlf)))
CLIPS> (myprint a b c)
a
b
c
CLIPS> (myprint (create$ a b) (create$ c d))
a
b
c
d
CLIPS>

Upvotes: 2

Related Questions