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. Please define predicates plus/3, times/3, and sum-up/2 (using recursion).
    Predicate sum-up(N,X) is fulfilled, if X equals the sum from 0 to N.

  6. Predicate fac/2 from exercise 1 can also be defined as follows:
      fac(X,N) :- fac(X,N,1).
    
      fac(0,A,A).
      fac(X,N,A) :- X > 0, A1 is A * X, X1 is X - 1, fac(X1,N,A1).
    
    Please define the predicates from exercise 5 and predicate fib/2 from exercise 1 using this technique. (Note: The third argument of of fac/3 is called accumulator.)