CS 3304 Homework #8

Date Assigned: November 5, 2003
Date Due: November 12, 2003, in class, before class starts
  1. (0 points) Correct the minor bug in the following append predicate developed and used copiously in class. You will need the corrected version to complete questions 2, 3, and 4.
    
    append([], X, X).
    append(X, [], X).
    append([X|Y], Z, [X|M]) :- append(Y, Z, M).
    
    Hint: If you can't figure out what the bug is, go ahead and use this predicate to solve questions 2, 3, and 4, and that will give you a hint about what is wrong.

  2. (5 points) Write a Prolog predicate makeset(L1, L2) that takes list L1 of integers and removes the repeating elements. The result is returned in list L2. So, this predicate has the same functionality as the ML makeset function you wrote for Homework #6.

  3. (5 points) Recall the triple predicate presented in class:
    
    triple(X, XXX) :- append(X, X, XX),
                      append(XX, X, XXX).
    
    If X=[1,2,3], this will produce [1, 2, 3, 1, 2, 3, 1, 2, 3] in XXX. Rewrite this predicate so that for X=[1, 2, 3], XXX is set equal to [1, 1, 1, 2, 2, 2, 3, 3, 3].

  4. (10 points) Recall the bubblesort code presented in class:
    
    bubblesort(X,Y) :- append(M, [A,B|N], X),
                       A > B,
                       append(M, [B,A|N], S),
                       bubblesort(S, Y).
    bubblesort(L,L).
    
    As demonstrated in class, after finding the right solution, the predicate goes on to find a lot of spurious solutions. "Fix" this code to only give the right solution. You are welcome to use cut (!) for this exercise.

  5. (30 points) You are given the following PROLOG clauses:
    
    female_author :- author(X), 
                     write(X), 
                     write(' is an author'),
                     nl,
                     female(X),
                     write(' and female'),
                     nl.
    
    female_author :- write('no such luck!'),
                     nl.
    
    author(X) :- name(X).
    
    author(X) :- write('no more found!'),
                 nl,
                 fail.
    
    name(sartre).
    name(calvino).
    name(joyce).
    
    female(murdoch).
    female(bembridge).
    
    Strategically place cuts (!) in the code to achieve each of the following desired outputs (i.e., each item below is a separate and independent part of the exercise). You are allowed to put only one cut per desired output. There are therefore five parts to this exercise.

    1. 
      sartre is an author
      no more found!
      no such luck!
      
    2. 
      sartre is an author
      calvino is an author
      joyce is an author
      no more found!
      
    3. 
      sartre is an author
      no such luck!
      
    4. 
      sartre is an author
      
    5. 
      sartre is an author
      calvino is an author
      joyce is an author
      no such luck!
      

Return Home