[ Term Manipulation | Reference Manual | Alphabetic Index ]

arity(+Term, -Arity)

Succeeds if Arity is the arity of Term.
Term
Prolog term.
Arity
Variable or integer.

Description

If Term is instantiated, its arity (number of arguments) is unified with Arity. For compound terms, this is the number of arguments, for atomic terms it is 0. As usual, non-empty lists are considered compound terms with arity 2.

Note that (like all predicates that return a number as their last argument), this predicate can be used as a function inside arithmetic expressions, e.g.

	..., (I > arity(Term) -> writeln(error), fail ; arg(I, Term, Arg) ).

Modes and Determinism

Exceptions

(4) instantiation fault
Term is uninstantiated (non-coroutine mode only).

Examples

Success:
   arity(f(1,2),2).
   arity(f(1,2),A).    (gives A=2).
   arity([],A).        (gives A=0).
   arity("s",A).     (gives A=0).
   arity(33,A).        (gives A=0).

Fail:
   arity(f(1,2),3).
   arity("compound(g)",1).

Error:
   arity(_,A).         (Error 4).

See Also

=.. / 2, arg / 3, functor / 3