CS323: PL&M Final Practice Exam Prof.: F.S. Annexstein Date : 5/2001 ======================================================================= 1. Write a scheme function CURRY that takes two arguments: a binary operator and an operand. This function should return a procedure of a single argument that returns the result of applying the operator to the operand and the argument of the procedure. 2. Consider the following Scheme code: (define int-builder (lambda (x) (list x (lambda () (int-builder (+ 1 x )))))) (define ints (int-builder 1)) Write a Scheme function that takes a stream (like ints above) and an integer n and return a list of the first n stream elements. (define stol (lambda (st n) .... 3. Write a Scheme mapping function apply-to-all$ for streams. (define apply-to-all$ (lambda (proc st) .... 4. Explain what a logic program is. Explain how the basic interpreter for a query in a logic program operates. 5. Use recursion to define the following relations on lists. a) append(A,B,C) if C is the same list as concatenating lists A with B. b) prefix(A,B) if list A is a prefix of B. c) suffix(A,B) if list A is a suffix of B. d) member(X,A) if X is an element of list A. e) delete(X,Y,Z) if Z is the list obtained by deleting X from list Y. f) ordered(A) if A is an ordered list of numbers. g) permutation(A,B) if B is a permutation of the list A. h) deleteall(X,Y,Z) if Z is the list obtained by deleting all occurences of X from list Y. i) deletesome(X,Y,Z) if Z is a list obtained by deleting zero, one, or more occurences of X from list Y. 6. Give all solutions produced by the following goal queries. ?:- append(U,V, [2,3,4,5]). ?:- permutation(X, [a,b,c]). 7a. Assume the parent(Parent,Child) relation has been defined. Use it to define the ancestor(Ancestor,Descendent) relation. This is the transitive closure of the parent relation. 7b. Assume the arc(X,Y) relation has been defined. Use it to define the connected(X,Y) relation. This is the transitive closure of the arc relation. 8. Explain the concept of nondeterministic programming. Give an example of a problem that can be solved using this technique. Show using logic programming how you would solve the problem. You need not give complete working code, just enough to see the structure of your program. 9. Consider the PROLOG segment given below and write the responses for the queries that follow. q1([]). q1([X]). q1(L):- append([F|M],[F],L),q1(M). q2([],[],[]). q2([X],[X],[]). q2([X,Y|L],[X|L1],[Y|L2]) :- q2(L,L1,L2). QUERIES: a) q1([m, a, d, a, m]). b) q1([a, r, i, s, t, o, t, l, e]). c) q2([p, r, e, s, i, d, e, n, t], L1, L2). d) q2([3, 4, 7, 1, 8, 9, 1, 2, 0, 12], L1, L2). Explain in words what the predicate q1 is. Explain in words what the relation q2 is. 10. Give the solution to the animal race puzzle given in class. ====================================================================