Next: Recursion Up: Prolog TermsBacktracking Previous: Declarative and Procedural

Some Exercises

To check you understand terms, unification and backtracking try the following. I may ask questions like these in the exam! (though they wouldn't be worth many marks).

1. Which of the following are valid Prolog terms:

2. Which of the following match, and for the ones that match, what are the resultant bindings.

3. For the following (silly) program, state what order the solutions will be returned given the query flies(X). (Your answer should be of the form X=soln1; X=soln2; etc).


  aeroplane(concorde).
  aeroplane(jumbo).
  on(fred, concorde).
  on(jim, no_18_bus).
  bird(percy).
  animal(leo).
  animal(tweety).
  animal(peter).
  has_feathers(tweety).
  has_feathers(peter).

  flies(X) :- bird(X).
  flies(X) :- aeroplane(X).
  flies(X) :- on(X, Y), aeroplane(Y).
  
  bird(X) :- animal(X), has_feathers(X).

Once you have worked out the answers by hand, check them using Prolog.


alison@
Fri Aug 19 10:42:17 BST 1994