CMPT 383

Summer 2009

 

Programming Assignment 2

Due: Monday, June 15, at 5:00 PM.

Due: Monday, June 22, at 5:00 PM. Submit your solution using the CSIL Submit Server .

Write a Prolog predicate rational such that

rational(X,Y)

will return values for X and Y such that X/Y is a rational number between 0 and 1, with X and Y being relatively prime positive integers. Each time you backtrack, rational(X,Y) should generate a new rational number. Every rational number should be generate eventually, and no rational number should be generate more than once.

To see what is expected, you may want to look at some output from my solution to this assignment. Other correct solutions may produce different results, for example by generating rational numbers in a different order.

You should not use either negation or the cut operator.

In program comments or a separate document, describe what happens if you call rational with either or both variables instantiated. Also state exactly the conditions under which a call to rational or backtracking to rational will initiate a nonterminating computation.


  1. Carefully read the subsection of the course web page on Format of Submitted Work.
  2. Notice that you are responsible for choosing appropriate test data.
  3. Carefully read the subsection of the course web page on Getting Help from Others.
  4. Look at the final sentence of the course web page.
  5. Do your assignment.

In this and other programming assignments, at least half your grade will depend on the clarity, simplicity and organization of your code, including the documentation (usually in the form of comments).