Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site burdvax.UUCP Path: utzoo!linus!philabs!seismo!presby!burdvax!puder From: puder@burdvax.UUCP Newsgroups: net.lang.prolog Subject: Re: Transitive Closures Message-ID: <1143@burdvax.UUCP> Date: Mon, 26-Sep-83 14:52:05 EDT Article-I.D.: burdvax.1143 Posted: Mon Sep 26 14:52:05 1983 Date-Received: Thu, 29-Sep-83 03:32:38 EDT References: <11903@sri-arpa.UUCP> Organization: SDC - a Burroughs Company, Paoli PA Lines: 69 Here is how I define the transitive closure and avoid infinite loops, etc. % cprolog CProlog version 1.3 | ?- [user]. | | % The base relation. | r(a,b). | r(b,c). | r(b,d). | r(f,a). | r(e,a). | | % The transitive relation. | tr(X,Y):-r(X,Y). | % Note that the next clauses call r and tr, they do not call tr twice! | tr(X,Y):-nonvar(X),!,r(X,Z),tr(Z,Y). | tr(X,Y):-r(Z,Y),tr(X,Z). | | user consulted 484 bytes 0.2 sec. yes | ?- tr(A,B). A = a B = b ; A = b B = c ; A = b B = d ; A = f B = a ; A = e B = a ; A = f B = b ; A = e B = b ; A = a B = c ; A = f B = c ; A = e B = c ; A = a B = d ; A = f B = d ; A = e B = d ; no | ?- -- Karl Puder {sdcrdcf,presby,psuvax}!burdvax!puder (215)648-7555