CS 2136 SAMPLE EXAM 1 November 11, 2003
All five problems count equally.
1. Define each of the following terms. (Just one or two sentences each).
a. progamming paradigm
b. first-order predicate calculus
c. head of a rule in Prolog
d. Horn clause
2. Write Prolog clauses to define the following predicates. You should only need to write one or two lines of code for each answer.
a) first(X, L) true if X is the first element on the list L.
b) factorial(X, Y) true if the number X equals Y! (i.e., X = 1* 2 * 3 ... * Y). You may assume that X ³ 1.
3. Our textbook calls the cut in Prolog extralogical. Explain what extralogical means and why cut is ex tralogical.
4. The following portion of a Prolog program has a cut in it.
average_taxpayer(X)
:- foreigner(X), !, fail.
average_taxpayer(X)
:- spouse(X,Y), gross_income(Y,Inc), I > 3000, ! fail.
average_taxpayer(X) :- gross_income(X,Inc), 2000 < Inc, 20000 > Inc.
a) Explain what the effect of the cut is.
b) Give a specific example in which the cut makes a difference. That is, give specific input for which the program would give different results with and without the cut, and explain why. To give a specific example, you may have to assume other clauses in the Prolog program.
5. Write a complete Prolog program to count the number of vowels in a list of letters. You should define a predicate numvowels which takes two arguments, L a list and N an integer, so that numvowels(L, N) is true if L is a list of letters and N is the number of vowels on the list. The vowels are a, e, i, o, and u. You may assume that all the letters on the list are lower case. You don't have to check that all the elements on the list are letters; you just have to count the number of vowels. For full credit, your program should work whether the second argument is a number or a variable.
So for example:
numvowels([a, b, c, d], 1) is true
numvowels([w, o, r, c, e, s, t, e, r], 3) is true
numvowels([w,p,i], N) should give the answer N = 1