Self-test 11 Solutions

Templates, determinism and non-determinism

     /* ************************************************ */      /*                                                  */      /*   memb_ext/4                                     */      /*      Arg 1: Element                              */      /*      Arg 2: List                                 */      /*      Arg 3: Atom, being a relation.              */      /*      Arg 4: Atom, either det or non.             */      /*   Summary: true if Arg 3 is an atom and          */      /*            memb_ext1/4 is true.                  */      /*   Author: P J Hancox                             */      /*   Date:   28 October 1994                        */      /*                                                  */      /* ************************************************ */      % 1      memb_ext(Elem, List, Relation, Det) :-           atom(Relation),           memb_ext1(Elem, List, Relation, Det).      /* ************************************************ */      /*                                                  */      /*   memb_ext1/4                                    */      /*      Arg 1: Element                              */      /*      Arg 2: List                                 */      /*      Arg 3: Atom, being a relation.              */      /*      Arg 4: Atom, either det or non.             */      /*   Summary: true if Arg 1 and the head of Arg2    */      /*            are related by Arg 3. Works deter-    */      /*            minstically if Arg 4 is det; non-     */      /*            determinstically if Arg4 is non.      */      /*   Author: P J Hancox                             */      /*   Date:   28 October 1994                        */      /*                                                  */      /* ************************************************ */      % 1 terminating      memb_ext1(Elem, [Head|_], Relation, _) :-           functor(Relation1, Relation, 2),           arg(1, Relation1, Elem),           arg(2, Relation1, Head),           call(Relation1).      % 2 recursive - deterministic      memb_ext1(Elem, [Head|List], Relation, det) :-           functor(Relation1, Relation, 2),           arg(1, Relation1, Elem),           arg(2, Relation1, Head),           \+ call(Relation1),           memb_ext1(Elem, List, Relation, det).      % 3 recursive - non-deterministic      memb_ext1(Elem, [_|List], Relation, non) :-           memb_ext1(Elem, List, Relation, non).
The main difference is the splitting the recursive clause into two. If the parameter is 'det' then the failure of the relation has to be tested (ie in the sub-goal \+ call(Relation1)); if the parameter is 'non' then there is no need to test the relationship.