Previous Up Next

4.9  Term processing

Apart from unification/matching, there are a number of generic built-in predicates that work on arbitrary data terms. The =.. predicate converts structures into lists and vice versa:

?- foo(a, b, c) =.. List.
List = [foo, a, b, c]
Yes (0.00s cpu)
?- Struct =.. [foo, a, b, c].
Struct = foo(a, b, c)
Yes (0.00s cpu)
The arg/3 predicate extracts an argument from a structure:

?- arg(2, foo(a, b, c), X).
X = b
Yes (0.00s cpu)
The functor/3 predicate extracts functor name and arity from a structured term, or, conversely, creates a structured term with a given functor name and arity:

?- functor(foo(a, b, c), N, A).
N = foo
A = 3
Yes (0.00s cpu)
?- functor(F, foo, 3).
F = foo(_1696, _1697, _1698)
Yes (0.00s cpu)
The term_variables/2 predicate extracts all variables from an arbitrarily complex term:

?- term_variables(foo(X, 3, Y, X), Vars).
Vars = [Y, X]
The copy_term/2 predicate creates a copy of a term with fresh variables:

?- copy_term(foo(3, X), Copy).
Copy = foo(3, _864)
Yes (0.00s cpu)

Previous Up Next