Programming Languages (CSC-302 98S)

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Assignments]

# Assignment Six: Logic Programming in Prolog

Assigned: Monday, April 27, 1998
Due: 11am, Monday, May 4, 1998

In all of the problems below, your testing should show not just that the predicates hold when given appropriate parameters, but also whether they can be used to generate appropriate values. If they cannot be used to generate values (e.g., most `reverse` predicates can only reverse one of their two parameters), make sure to note these limitations.

1. Reversing Lists

Define and test a binary predicate, `reverse(L1,L2)`, that holds between two lists if and only if they contain the same elements, but in the opposite order.

2. Insertion

Define and test a trinary predicate, `insert(Old,Elt,New)`, that holds if and only if `New` is the same as `Old` except that one copy of `Elt` has been added at some position (not necessarily the front). For example,

```insert([a,a], b, [b,a,a])
```

and
```insert([a,a], b, [a,b,a])
```

and
```insert([a,a], b,[a,a,b])
```

3. Removal

Define and test a trinary predicate, `remove(Old,Elt,New)`, that holds if and only if `New` is the same as `Old` except that one copy of `Elt` has been removed.

4. Permutations

Define and test a binary predicate, `permutation(L1,L2)`, that holds between two lists if and only if they contain the same elements (but possibly in another order).

5. Sorting Lists

Define and test a binary predicate, `sorted(L1,L2)`, that holds between two lists if, and only if, the second contains the same elements as the first, but in sorted order. You may use any strategy you deem appropriate, but you should indicate what the approximate running time is in big-O notation.

6. Inserting Elements in Search Trees

Write and test a trinary predicate, `insert(Old,Elt,New)`, that holds if and only if `New` results from inserting `Elt` in the search tree `Old`. Recall that a search tree is a binary tree in which the left subtree of a node contains values smaller than the node and the right subtree contains values bigger than the node. You can use `node(Val,Left,Right)` and `empty` as your tree constructors.

7. Building Search Trees

Write and test a binary predicate, `build(Tree,List)`, that holds if and only if `Tree` is a search tree that can be built from `List` by inserting the elements of `List` into the empty tree in order. (Hmmm ... sounds more like a procedure than a predicate, doesn't it?)

8. Relating Predicates

What is the relationship between the `insert` and `remove` predicates you wrote in parts 2 and 3? Should they be the same? Are they?

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Assignments]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.