[ Term Manipulation | Reference Manual | Alphabetic Index ]

term_variables(?Term, -VarList)

Succeeds if VarList is the list of all variables in Term.
Term
Prolog term.
VarList
List or variable.

Description

This predicate collects all the variables inside Term into the list VarList. Every variable occurs only once in VarList, even if it occurs several times in Term. The order of the variables in the list is not specified.

As usual, attributed variables are also considered variables.

This predicate terminates even with cyclic terms.

Modes and Determinism

Exceptions

(5) type error
VarList instantiated but not to a list.

Examples

Success:
    term_variables(atom, []).
    term_variables(Term, Vs).       % gives Vs = [Term]
    term_variables(f(a,B,c), Vs).   % gives Vs = [B]
    term_variables([X,Y,Z], Vs).    % gives Vs = [Z,Y,X]
    term_variables([X,Y,X], Vs).    % gives Vs = [Y,X]
    term_variables(s(X{a}), Vs).    % gives Vs = [X{a}]

Fail:
    term_variables(f(a,B,c), []).

See Also

term_variables_array / 2, nonground / 1, nonground / 2, nonground / 3, nonvar / 1, var / 1