# Assignment Five: Logic Programming in Prolog

Assigned: Monday, April 12, 1998
Due: start of class, Monday, April 19, 1998

This is your final written assignment for CS302. You should spend the remaining ``assignment time'' on your presentations.

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 if list `L1` is the reverse of `L2`. That is, it 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])
```

all hold. However

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

and

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

should both fail.

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 when list `L2` is a permutation of `L1` (or vice versa). That is, `permutation(L1,L2)` holds if and only if `L1` and `L2` contain exactly 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.

History

• Created Monday, April 12, 1999. Based on assignment 6 of CS152 98S. (Removed some problems; clarified others.)

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.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS302/99S/Assignments/assign.05.html

Source text last modified Mon Apr 12 08:48:25 1999.

This page generated on Mon Apr 12 08:54:33 1999 by SiteWeaver. Validate this page's HTML.

Contact our webmaster at rebelsky@math.grin.edu