CS334 Assignment 9

CS334 PROGRAMMING LANGUAGES

Assignment 9 ----------------------------- Due 5/13/96

Problems 4-6 may be turned in as late as the beginning of class on Friday, 5/16, but 1-3 (along with the big project from last week) must be turned in on 5/13.

  1. Write a PROLOG program fib, such that fib(n,fn) is true iff fn is the nth Fibonacci number. Recall that fib0 = 1, fib1 = 1, and for all n > 1, fibn = fibn-1 + fibn-2. (Remember to use "is" in order to force the computation.)

  2. a. Given a list of the form

    [item(name1,price1),..., item(namek,pricek)],

    write program cost(L,Name,Price) which given a list L (as above) and Name, gives the price of that item.

    b. Can this program be run backwards? That is, what happens when you provide the list L and Price, but not the Name?

    c. Write a program like that in (a), except that it takes as an argument a list of names rather than a single name. That is, write a program listcost(L,Names,Price), where L is as above, Names is a list of names of items, and Price is the total cost of all of the items in Names. Can it be run backwards?

  3. Please do problems 22 and 28 on pages 455 and 456 of the text.

  4. a. Write a program to take derivatives of polynomials. That is, if a list of the form [a0,a1,...,ak] represents the polynomial a0 + a1x + ... + akxk, then write a program which takes a list representing a polynomial and returns a list representing its derivative.

    b. See if you can run this program backwards to integrate. (Feel free to make the constant term of the integral 0). If you cannot, what goes wrong? (You do not need to modify the program written in part a in a tricky way to make it run backwards. If it won't go backwards, just explain whycx!)

  5. Negation as cut has some very strange properties in PROLOG. For example the program given by:
    		?- not(X=1),X=2.
    typically gives a different answer than the same query in reverse, i.e.
    		?- X=2, not(X=1).
    Run these programs and explain the different answers that you obtain. (See the solution to problem 31 on page 456 of the text for a hint.)

  6. Write a PROLOG program to complete the solution to the Russian Farmer problem given in class.