Self-test 9 Solutions

Multiple solutions in list processing

     /* ************************************************ */      /*                                                  */      /*   delete_all/3                                   */      /*      Arg 1:                                      */      /*      Arg 2:  List                                */      /*      Arg 3:  List                                */      /*   Summary: true if Arg 3 is Arg 2 with all       */      /*            instances of Arg 1 deleted.           */      /*   Author: P J Hancox                             */      /*   Date:   28 October 1994                        */      /*                                                  */      /* ************************************************ */      % 1 terminating condition      delete_all(_, [], []).      % 2 recursive - Elem matches head      delete_all(Elem, [Elem|Tail1], Tail2) :-           delete_all(Elem, Tail1, Tail2).      % 3 recursive - Elem doesn't match head      delete_all(Elem, [Head|Tail1], [Head|Tail2]) :-           \+ (Elem = Head),           delete_all(Elem, Tail1, Tail2).
The terminating condition is now the empty list because we need to work through the entire list to find all elements. There are two recursive conditions: one for when Elem unifies with the head of the list and a second for when Elem and the Head of the list don't unify.