next up previous

9.7.5 Scope in Instance-set Query Functions

An instance-set query function can be called from anywhere that a regular function can be called. If a variable from an outer scope is not masked by an instance-set member variable, then that variable may be referenced within the query and action. In addition, rebinding variables within an instance-set function action is allowed. However, attempts to rebind instance-set member variables will generate errors. Binding variables is not allowed within a query. Instance-set query functions can be nested.

Example

CLIPS>
(deffunction count-instances (?class)
  (bind ?count 0)
  (do-for-all-instances ((?ins ?class)) TRUE
    (bind ?count (+ ?count 1)))
 ?count)
CLIPS>
(deffunction count-instances-2 (?class)
  (length (find-all-instances ((?ins ?class)) TRUE)))
CLIPS> (count-instances WOMAN)
3
CLIPS> (count-instances-2 BOY)
4
CLIPS>

Instance-set member variables are only in scope within the instance-set query function. Attempting to use instance-set member variables in an outer scope will generate an error.

Example

CLIPS>
(deffunction last-instance (?class)
   (any-instancep ((?ins ?class)) TRUE)
   ?ins)
[PRCCODE3] Undefined variable ins referenced in deffunction.
ERROR:
(deffunction last-instance
   (?class)
   (any-instancep ((?ins ?class))
      TRUE)
   ?ins
   )
CLIPS>


next up previous