Self-Test 9

Multiple solutions in list processing
 
This question includes a partial program. You are advised to copy and paste this into a file. You should develop your solution on a machine and test it before looking at the solution.
 
 

A procedure for deleting an element from a list is given as:

     % 1 terminating condition
     delete_element(Elem, [Elem|Tail], Tail).
     % 2 recursive
     delete_element(Elem, [Head|Tail1], [Head|Tail2]) :-
          delete_element(Elem, Tail1, Tail2).
This is non-determinstic and so will delete each element in the list in turn when backtracking:
     | ?- delete_element(a, [a,b,a,b], List).

     List = [b,a,b] ? ;

     List = [a,b,b] ? ;

     no
Write a version of delete_element/3 called delete_one/3 which will delete only one element, for instance:
     | ?- delete_one(a, [a,b,a,b], List).

     List = [b,a,b] ? ;

     no
 
 
  Write a procedure called replace/4 that is true if an element in List1 at position Pos is replaced by Elem to give List2.
 


Write a version of delete_element/3 called delete_all/3 which will delete all matching elements, for instance:

     | ?- delete_all(a, [a,b,a,b], List).

     List = [b,b] ? ;

     no
     | ?- delete_all(c,[a,b,b,a],List).

     List = [a,b,b,a] ? ;

     no
     | ?- delete_all(_, [a,b,a,b], List).

     List = [b,b] ? ;

     no
This needs a little thought. If you can't see how to structure the answer, try a hint.