Programming Languages
Prolog Test
Sample Questions

1. Assuming that this program has been loaded into the Prolog interpreter:

     circle(a).
     circle(b).
     triangle(c).
     square(d).
     red(X) :- circle(X).
     red(X) :- square(X).
     yellow(X) :- triangle(X).
     p(X,Y) :- circle(X), circle(Y).
     q(X,Y) :- red(X), yellow(Y).
     r(X) :- circle(X), yellow(X).
circle each of the goals below which will succeed when given to the interpreter.
     red(c).					
     yellow(c).					
     red(a).
     yellow(b).
     p(a, b).
     q(b, c).
     p(a, a).
     q(c, a).
     r(b).
     r(d).

2. Assuming that this program has been loaded into the Prolog interpreter:

     p([ ]).
     p([H | T]) :- H > 5, H < 8 , p(T).
circle each of the goals below which will succeed when given to the interpreter.
     p([7]).
     p([8, 7, 6]).
     p([1, 3, 5, 7, 9]).
     p([6, 7]).
     p([10, 6, 2]).

3. Assuming that this program has been loaded into the Prolog interpreter:

     q([1], [1]).
     q([1, 1 | T1], [1 | T2]) :- q(T1, T2).
circle each of the goals below which will succeed when given to the interpreter.
     q([1, 1], [1, 1, 1]).
     q([1, 1, 1, 1], [1, 1]).
     q([1, 1, 1], [ ]).
     q([1, 1, 1, 1, 1], [1, 1, 1]).
     q([1, 1, 1], [1, 1]).

4. Write a Prolog predicate pos_discriminant(A, B, C) which will succeed if B 2 - 4 A C is positive or zero, and which will fail otherwise. For example, pos_discriminant(0, 0, 0) should succeed (because B 2 - 4 A C = 0), and pos_discriminant(1, 10, 1) should succeed (because B 2 - 4 A C = 100 - 4 = 96), but pos_discriminant(10, 1, 1) should fail (because B 2 - 4 A C = 1 - 40 = -39).

5. Write a Prolog predicate abc(L) which succeeds if list L contains an a, followed directly by a b, followed directly by a c; and which fails otherwise. For example, abc([d,a,b,c,e,f]) should succeed, and abc([d,a,b,g,c,e,f]) should fail.


Nancy Tinkham
Computer Science Department, Rowan University

Valid HTML 4.0!