CSC 270
Homework 9 in Prolog

Assigned Dec 3, due Dec 10

As before, all programs should come with test cases, either automated or in comments.

    Warm-up Exercises (ungraded unless you specifically ask)

  1. Adventure in Prolog chap. 10 "nonsense Prolog" exercises

  2. Adventure in Prolog chap. 11 exercises 3 and 4

  3. Lists

  4. Write a predicate listLength which succeeds iff its first parameter is a list, and its second is the length of the list. (There's a built-in named length already.) It only needs to work if the list is completely bound.
    (Extra credit: it works even if the list is partially or completely unbound.)

  5. Write a predicate elementAfter which succeeds iff its second parameter is a list, and its third is the element after the first occurrence of the first parameter in that list. (If the first parameter doesn't occur, or if it occurs only at the end, elementAfter should fail.) It only needs to work if the list is completely bound.
    (Extra credit: it works even if the list is partially or completely unbound.)

  6. Write a predicate addUp which succeeds iff its first parameter is a list of numbers and its second is their sum. It only needs to work if the list is completely bound.
    (Extra credit: it works even if the list is partially or completely unbound.)

  7. Write a predicate countMatches which succeeds iff its second parameter is a list and its third parameter is the number of times the first parameter occurs in that list. It only needs to work if the list is completely bound.
    (Extra credit: it works even if the list is partially or completely unbound.)

  8. Write a predicate convertReversed which succeeds iff its first parameter is a list of integers in the range 0-9 and its second parameter is the number they represent in decimal, interpreting the first digit as the least significant. (For example,

    convertReversed([3,2,7], 723).
    should succeed.) It only needs to work if the list is completely bound.
    (Extra credit: it works even if the list is partially or completely unbound; used this way, it essentially converts numbers to decimal.)

  9. Write a predicate allMatch which succeeds iff its second parameter is a list of which every element matches the first parameter. It only needs to work if the list is completely bound.
    (Extra credit: it works even if the list is partially or completely unbound.)

  10. Write a predicate largest which succeeds iff its first parameter is a non-empty list of numbers and the second parameter is the largest number in the list. It only needs to work if the list is completely bound.
    (Extra credit: it works even if the list is partially or completely unbound.)

  11. Write a predicate squareEach which succeeds iff both parameters are lists of numbers, the same length, and each element of the second parameter is the square of the corresponding element of the first parameter. It only needs to work if the first list is completely bound.
    (Extra credit: it works even if the first list is partially or completely unbound.)

  12. Write a predicate substitute which succeeds iff its third and fourth parameters are lists, equal to one another except that all occurrence of the first parameter appearing in the third are replaced in the fourth parameter by the second. It only needs to work if the first list is completely bound.
    (Extra credit: it works even if the first list is partially or completely unbound.)

  13. Six Degrees of Kevin Bacon, revisited

  14. Based on your degrees predicate from Homework 8, write a path predicate that takes in two actors' names and binds the third parameter to a list showing a link from one to the other. At a minimum, this list should indicate each actor's name along the way; it would be cooler if it showed not only each actor's name but what movie they were both in, at each step. For example, you could do it as a list of alternating actors' names and movie names, e.g.

    path('Harrison Ford', 'Kevin Bacon',
       ['Harrison Ford', 'Star Wars', 'Carrie Fisher', 'The Blues Brothers', 'John Belushi', 'Animal House', 'Kevin Bacon']).

  15. Binary search trees

    Define a "binary search tree" data type as in homework 2, with the following predicates:

  16. binaryTree, which tells whether something is a binary tree at all.

  17. bst, which tells whether something is a "proper" binary search tree (as defined in homework 2). If it's not completely bound (or if it isn't a binary tree), it doesn't qualify as proper.

  18. inBST, which tells whether a given (bound) number occurs in a given BST (which you may assume is completely bound and proper)

  19. pathTo, which finds the path to a given (bound) number in a given BST, or to where the number would be if it were in the BST. Again, you may assume the BST is completely bound and proper.

  20. insert, which inserts a new (bound) number into a given BST. If the number is already there, the result should be the same as the original BST. You may assume the "given BST" is completely bound and proper.

  21. makeBST, which takes in a list of numbers and produces a BST containing exactly those numbers. You may assume the list is completely bound.

  22. Cut and higher-order predicates

    We've been writing test predicates that throw a bunch of bound values at a predicate and check whether it succeeds or fails. But sometimes you want to test a predicate on unbound parameters to see whether it generates the right answer. Here's a step in that direction.

  23. Write a firstResult predicate which takes in a question, a variable name (which presumably appears in the question), and a value; it asks the question with the variable unbound, and then checks whether the variable was bound to the specified value. For example,

    firstAnswer(convertReversed(Digits,30150), Digits, [0,5,1,0,3]).

  24. Difference lists

    I might have an exercise or two on this...


Last modified:
Stephen Bloch / sbloch@adelphi.edu