CSE 526: Principles of Programming Languages, Spring 2019

Homework #1

Due: Tue., Feb 18


This is a Prolog programming assignment. You are expected to write a Prolog program that consists of predicates described below. Unless otherwise specified, the solution to each question may have multiple Prolog predicates. You may also use the solution to one question to solve another question. Place all the Prolog predicate definitions you write to solve the following problems in a single file called hw1.P or hw1.p, depending on the Prolog system you used, and submit the file via Blackboard (hand-in procedure is described at the end of this handout).

    Part A (Warm-Up):

  1. order: Write a predicate order(L, X, Y) that succeeds if and only if elements X and Y occur in list L such that X occurs before Y. For instance:

  2. split: Write a predicate split(L, X, Pre, Succ) that succeeds if and only if element X occurs in list L, and Pre is a list of elements that occur before X in L, and Succ is a list of elements that occur after X in L. The order of elements in Pre and Succ are same as that in L. For instance:

  3. rotate: Write a predicate rotate(L1, L2) that succeeds if and only if list L1 is a rotation of list L2, i.e., if L1 = [x1, x2, x3, ..., xn], then L2 = [xi, xi+1, ..., xn, x1, x2, ..., xi-1] for some 1 ≤ i ≤ n For instance:

    Part B (Trees):

    For the following five questions, consider using Prolog terms to represent binary trees as follows:

    balanced tree unbalanced/ordered tree balanced/ordered tree
    (a) (b) (c)

    For instance,

  4. isTree: Write a predicate isTree(T) that succeeds if and only if T is a binary tree.

    Clearly, the predicate isTree should succeed for the example term for tree(a) given above. isTree will fail for node(3, 1, 5), node(1, emty, empty), and node(2, node(1, empty), empty).

  5. balanced: Write a predicate balanced(T) that succeeds if and only if T is a binary tree that is height-balanced: i.e., the lengths of the longest root-to-leaf path and the shortest root-to-leaf path differ by at most one.

    For example, trees (a) and (c) in the picture above are balanced.

  6. ordered: Write a predicate ordered(T) that succeeds if and only if an in-order traversal of T spells out an increasing sequence of values. For this part, you may assume that all values in T are comparable with "<".
    Note: In-order traversal of a tree traverses, at each level, the left subtree, the root, and then the right subtree.

    For example, trees (b) and (c) in the above picture are ordered.

  7. build: Write a predicate build(L, T), that, given a list L constructs a tree T such that an in-order traversal of T spells out L.

    For example, build([1,2,3,5,8,13,21], T) can give, as T tree (b) in the picture above, as one possible answer.

  8. build_balanced: Write a predicate build(L, T), that, given a list L constructs a balanced tree T such that an in-order traversal of T spells out L.

    For example, build_balanced([1,2,3,5,8,13], T) can give, as T tree (c) in the picture above, as one possible answer.

Grading:

There are 8 questions in all, worth 5 points each. Note: not all questions are equally hard, but they are all worth the same number of points.

Submission:

Submit your homework via Blackboard. In "Assignments" area there you will see a form for HW1. Fill it out by uploading the file containing your Prolog program.

The file must be named hw1.P or hw1.p

Make sure you submit the form.

Errata:

Earlier versions had the following errors that have since been corrected:
  1. Mon Feb 10 19:21:18 EST 2020: Examples in Q1 and Q3 had minor errors that have been fixed now.
  2. Wed Feb 12 20:13:57 EST 2020: Examples before Q4, the representation of Tree (c) had an error that's now fixed.

C.R. Ramakrishnan
Last modified: Wed Feb 12 20:13:57 EST 2020