Simply Logical lab exercises chapter 3

The Prolog file to be used for this chapter's lab exercises is

You can trace Prolog's computations by giving the command ?-trace. This will put Prolog in trace mode, showing every single resolution step (except for the predicates that have been compiled rather than consulted). Trace mode is switched off by the command ?-notrace. An example of a trace is given here.

  1. defines three predicates sublist1(SL,L), sublist2(SL,L), and sublist3(SL,L), each of which generates sublists of a given list L. (NB. You will also need the files and First, make sure that you understand the order of the solutions to the query ?-sublist1(SL,[a,b]).
    (NB. if you are using the tracer, type s for skip instead of RETURN whenever you encounter an append goal, to avoid expanding the calls to append.)
    Also, check the reason for the non-terminating behaviour of sublist2 after all answers to the previous query have been found.

    These three predicates have the disadvantage that the empty list is generated n+1 times, where n is the length of the second argument. Change each of the definitions to avoid this.
    (Hint: treat the empty sublist as special case, making sure that the given clauses don't succeed for the emtpy list.)

  2. Write a predicate subsequence(SS,L) such that SS is a list of elements occurring in that order, but not necessarily successively, in list L.
    For example:

  3. Two lists are disjoint if they don't have an element in common. Define the Prolog predicate disjoint(L1,L2) using not and element (defined in

  4. Write a predicate element1(X,L) that succeeds if X occurs exactly once in list L.

    Does your definition generate all solutions of the query ?-element1(X,[a,b,b,c]).? If not, why not?

Back / Peter Flach