Availability:built-in
setarg(+Arg,
+Term, +Value)Extra-logical predicate. Assigns the Arg-th argument of the
compound term Term with the given Value. The
assignment is undone if backtracking brings the state back into a
position before the
setarg/3
call. If the designated argument of Term is a variable, this
variable is unified with Value using normal unification,
i.e., setarg/3
behaves as arg/3
in this case. Note that this may produce a cyclic term if Value
contains this variable. See also
nb_setarg/3.
This predicate may be used for destructive assignment to terms, using
them as an extra-logical storage bin. Always try hard to avoid the use
of setarg/3
as it is not supported by many Prolog systems and one has to be very
careful about unexpected copying as well as unexpected noncopying of
terms. A good practice to improve somewhat on this situation is to make
sure that terms whose arguments are subject to setarg/3
have one unused and unshared variable in addition to the used arguments.
This variable avoids unwanted sharing in, e.g., copy_term/2,
and causes the term to be considered as non-ground. An alternative is to
use put_attr/3
to attach information to attributed variables (seeĀ section
8.1).