This predicate collects all the variables inside Term into an array VarArr. Every variable occurs only once in VarArr, even if it occurs several times in Term. The order of the variables in the array corresponds to the order in which they are first encountered during a left-to-right, depth-first traversal of Term.
As usual, attributed variables are also considered variables.
This predicate terminates even with cyclic terms.
Success: term_variables_array(atom, []). term_variables_array(Term, Vz). % gives Vz = [](Term) term_variables_array(f(a,B,c), Vz). % gives Vz = [](B) term_variables_array([X,Y,Z], Vz). % gives Vz = [](X,Y,Z) term_variables_array([X,Y,X], Vz). % gives Vz = [](X,Y) term_variables_array(s(X{a}), Vz). % gives Vz = [](X{a}) Fail: term_variables_array(f(a,B,c), []).