Variables are distinguished by an initial capital letter or by the initial character ``_'' for example
A variable should be thought of as standing for some definite but unidentified object. A variable is not simply a writable storage location as in most programming languages; rather it is a local name for some data object, cf. the variable of pure LISP and constant declarations in Pascal.
The structured data objects of the language are the compound terms. A compound term comprises a functor (called the principal functor of the term) and a sequence of one or more terms called arguments. A functor is characterized by its name, which is an atom, and its arity or number of arguments. For example the compound term whose functor is named `point' of arity 3, with arguments X, Y and Z, is written
A functor or predicate symbol is uniquely identified by its name and arity (in other words, it is possible for different symbols having different arities to share the same name). A functor or predicate symbol p with arity n is usually written p/n.
One may think of a functor as a record type and the arguments of a compound term as the fields of a record. Compound terms are usefully pictured as trees. For example, the term
s / \ np vp | / \ john v np | | likes mary |
Sometimes it is convenient to write certain functors as operators — 2-ary functors may be declared as infix operators and 1-ary functors as prefix or postfix operators. Thus it is possible to write
Lists form an important class of data structures in Prolog.
They are essentially the same as the lists of LISP:
a list either is the atom [ ],
representing the empty list, or is a compound term with functor `.'/2
and two arguments which are respectively the head and tail of the list.
Thus a list of the first three natural numbers is the structure
(shown in Figure ) which could be written,
using the standard syntax, as .(1,.(2,.(3,[ ]))),
[X|L] [a,b|L] |
Note that this list syntax is only syntactic sugar for terms of the form `.'(_, _) and does not provide any new facilities that were not available otherwise.
For convenience, a further notational variant is allowed for lists of integers which correspond to ASCII character codes. Lists written in this notation are called strings. For example, "Prolog" represents exactly the same list as [80,114,111,108,111,103].