   Next: Lists Up: Prolog Terms Previous: Variables

## Compound Terms

Compound terms allow the representation of data with substructure. A compound term consists of a functor followed by a sequence of one or more subterms called arguments. It sometimes helps to think of a compound term as a tree structure. For example, the term
`sentence(nphrase(john),vbphrase(verb(likes),nphrase(mary)))` could be depicted as the structure:

```
______sentence______
|                    |
nphrase         _____vbphrase_____
|           |                  |
john        verb              nphrase
|                  |
likes              mary
```
where sentence is the principal functor of the term and its arguments are `nphrase(john)` and `vbphrase(verb(likes),nphrase(mary))`. Each argument in this example is also a term with functor name and arguments.

When we want to refer to a particular class of term without giving its precise definition, we give its principal functor and its arity (the number of arguments which it takes). The example above has principal functor sentence and arity 2. The shorthand notation for this is `sentence/2`.

The predicate `functor(Term, F, A)` succeeds if `Term` has functor `F` and arity `A`. Its behaviour is as follows:

```
| ?- functor(sentence(nphrase(john),vbphrase(verb(likes),nphrase(mary))),
|:           Functor, Arity).
Functor=sentence
Arity=2
yes
| ?- functor(foo, Functor, Arity).
Functor=foo
Arity=0
yes
```

Dave Stuart Robertson
Tue Jul 7 10:44:26 BST 1998