Concepts of Programming Languages

(Sean Pound) #1
16.6 The Basic Elements of Prolog 749

(2) 2 Call: append([jo], [jake, darcie], _18)?
(3) 3 Call: append([], [jake, darcie], _25)?
(3) 3 Exit: append([], [jake, darcie], [jake, darcie])
(2) 2 Exit: append([jo], [jake, darcie], [jo, jake,
darcie])
(1) 1 Exit: append([bob, jo], [jake, darcie],
[bob, jo, jake, darcie])
Family = [bob, jo, jake, darcie]
yes


The first two calls, which represent subgoals, have List_1 nonempty, so they
create the recursive calls from the right side of the second statement. The
left side of the second statement effectively specifies the arguments for the
recursive calls, or goals, thus dismantling the first list one element per step.
When the first list becomes empty, in a call, or subgoal, the current instance
of the right side of the second statement succeeds by matching the first state-
ment. The effect of this is to return as the third parameter the value of the
empty list appended to the second original parameter list. On successive exits,
which represent successful matches, the elements that were removed from
the first list are appended to the resulting list, Family. When the exit from
the first goal is accomplished, the process is complete, and the resulting list
is displayed.
Another difference between Prolog’s append and those of LISP and ML is
that Prolog’s append is more flexible than that of those languages. For exam-
ple, in Prolog we can use append to determine what two lists can be appended
to get [a, b, c] with


append(X, Y, [a, b, c]).


This results in the following:


X = []
Y = [a, b, c]


If we type a semicolon at this output we get the alternative result:


X = [a]
Y = [b, c]


Continuing, we get the following:


X = [a, b]
Y = [c];
X = [a, b, c]
Y = []

Free download pdf