Exercises (Prolog)

  1. A predicate length/2 describing the length of a list can be defined as follows.
      length(0,[]).
      length(N,[_|L]) :- length(M,L), N is M+1.
    
    Please define predicates member/2, fac/2, fib/2, and gcd/3 analogously.

  2. Suppose predicates parent/2, female/1 i male/1 with the obvious interpretation are given.
    Please define predicates child/2, mother/2, sister/2, has_a_child/1, grandparent/2, and predecessor/2.

  3. Suppose the following predicate f/2 is given.
     f(1,one).
     f(s(1),two).
     f(s(s(1)),three).
     f(s(s(s(X))),N) :- f(X,N). 
    How does Prolog answer to the following questions?

    1. f(s(1),A)?
    2. f(s(s(1)),two)?
    3. f(s(s(s(s(s(s(1)))))),C)?
    4. f(D,three)?

  4. Please define a predicate latin/2, which transforms Latin numbers into arabic numbers. Latin numbers are represented by ordinary lists,
    for example [x,l,v,i,i] denotes 47. You can assume that the Latin number is correct.

  5. Assume the following predicates are given.
     q1(X,Y) :- p(X,Y).                q2(X,Y) :- p(X,Z), q2(Z,Y).
     q1(X,Y) :- p(X,Z), q1(Z,Y).       q2(X,Y) :- p(X,Y).  
    
     q3(X,Y) :- p(X,Y).                q4(X,Y) :- q4(X,Z), p(Z,Y).  
     q3(X,Y) :- q3(X,Z), p(Z,Y).       q4(X,Y) :- p(X,Y). 
        
     p(tom,bob).
     p(tom,liz).
     p(bob,ann).      
     p(bob,pat).     
     p(pat,jim). 
    Using answer trees please show how Prolog answers to the following questions: ?-qi(tom,pat). and ?-qi(liz,jim) for i = 1, ... 4.