Each node has a number, so we could start by a functor and a first
argument:
node(a, ...
The first argument is the "name" of the node: the remaining arguments
are used to represent each node linked to the first node.
node(a, 1, 3, 6).
node(1, 2, 4).
node(2, 5).
node(3, 4, 7).
What are the arities of these objects? They are as follows:
node(a, 1, 3, 6). arity 4
node(1, 2, 4). arity 3
node(2, 5). arity 2
node(3, 4, 7). arity 3
...
We have three different arities, even though we have represented only
four nodes so far. It is possible to write Prolog programs that can
handle structured objects with differing arities that represent the
same kind of object, but it makes the program more complicated than
necessary. (We will see when we study list processing in Prolog that
it is possible to represent the destination as a single list of nodes,
which would reduce the arity of node to two, but it
would still be more complicated to program than the method of encoding
the paths.) So the important principle is that we should choose a
form of structured object that is as simple as possible, while allowing
us to write the most efficient program.
|