next up previous

12.18 SEQUENCE EXPANSION

In the past, there has been no distinction between single-field and multifield variable references within function calls (as opposed to declaring variables for function parameters or variables used for pattern-matching). For example, for the rule:

(defrule expansion
  (foo $?b)
  =>
  (printout t ?b crlf)
  (printout t $?b crlf))

would have given identical output for both printout statements in version 5.1 of CLIPS.

CLIPS> (assert (foo a b c))
<Fact-0>
CLIPS> (run)
(a b c)
(a b c)
CLIPS>

Multifield variable references within function calls are now treated differently. The $ acts as a "sequence expansion" operator and has special meaning when applied to a global or local variable reference within the argument list of a function call. The $ means to take the fields of the multifield value referenced by the variable and treat them as separate arguments to the function as opposed to passing a single multifield value argument.

For example, using sequence expansion with the expansion rule would give the following output:

CLIPS> (assert (foo a b c))
<Fact-0>
CLIPS> (run)
(a b c)
abc
CLIPS>

Using sequence expansion, the two printout statements on the RHS of the expansion rule are equivalent to:

(printout t (create$ a b c) crlf)
(printout t a b c crlf)

The $ operator also works with global variables. For example:

CLIPS> (defglobal ?*x* = (create$ 3 4 5))
CLIPS> (+ ?*x*)
ERROR: Function + expected at least 2 argument(s)
CLIPS> (+ $?*x*)
12
CLIPS>

The sequence expansion operator is particularly useful for generic function methods. Consider the ease now of defining a general addition function for strings.

(defmethod + (($?any STRING))
   (str-cat $?any))

By default, sequence expansion is disabled. This allows previously existing CLIPS programs to work correctly with version 6.0 of CLIPS. The behavior can be enabled using the set-sequence-operator-recognition function described in section 12.18.3. Old CLIPS code should be changed so that it works properly with sequence expansion enabled.

12.18.1 Sequence Expansion and Rules

12.18.2 Multifield Expansion Function

12.18.5 Sequence Operator Caveat



next up previous