Article View: comp.lang.prolog
Article #81Re: transitive and symetric relations
From: Ken Johnson
Date: Tue, 11 Aug 1992 10:18
Date: Tue, 11 Aug 1992 10:18
58 lines
2380 bytes
2380 bytes
In article <SWK.92Aug10220513@seymour.mlb.semi.harris.com> swk@seymour.mlb.semi.harris.com (Song W. Koh) writes: # I am having trouble describing transitive and # symmetric relations without stack overflows. Things like: # # distance(X,Y,D) :- distance(Y,X,D). The key is to use a different functor for the DATA and the PROGRAM. If you use distance/3 to specify data and distance_between/3 to describe the symmetric relation, then the problem will go away. distance_via/4 I've included for interest; it finds the distance from A to B via exactly one intermediate point. If you don't know how many intermediate points there will be on the journey, you cannot do it this way; you will want a solution that uses lists. (If you can't do it yourself, ask me and I will post you a version that does it that way.) This code was tested on Edinburgh Prolog version 1.5.04, version 12 September 1988. % Ken Johnson 11 August 1992 % % You may use and reproduce this code freely but if you sell copies at a % profit I want a share. distance_between(A,B,N) :- % Distance on a one-step journey distance(A,B,N). distance_between(A,B,N) :- distance(B,A,N). distance_via(A,Via,B,D) :- % Distance on a 2-step journey distance_between(A,Via,D1), distance_between(Via,B,D2), D is D1 + D2. % The following distances are not meant to be accurate! distance(edinburgh,glasgow,40). distance(edinburgh,london,400). distance(edinburgh,newcastle,100). distance(edinburgh,inverness,80). distance(inverness,wick,80). distance(london,dover,50). distance(london,penzance,250). distance(dover,calais,20). -- //// Advice to dieters: //// Ken Johnson, A I Applications Institute //// Never eat more than //// 80 South Bridge, EDINBURGH EH1 1HN //// you can carry. //// E-mail ken@aiai.ed.ac.uk //// -- Miss Piggy //// phone 031-650 2756 fax 031-650 6513
Technical Details
Message-ID:
<7140@skye.ed.ac.uk>Path:
arc1-ca.pugleaf.net!sparky!uunet!mcsun!uknet!edcastle!aiai!kenReferences:
<SWK.92Aug10220513@seymour.mlb.semi.harris.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!uknet!edcastle!aiai!ken From: ken@aiai.ed.ac.uk (Ken Johnson) Newsgroups: comp.lang.prolog Subject: Re: transitive and symetric relations Message-ID: <7140@skye.ed.ac.uk> Date: 11 Aug 92 10:18:19 GMT References: <SWK.92Aug10220513@seymour.mlb.semi.harris.com> Followup-To: comp.lang.prolog Distribution: comp.lang.prolog Organization: Bugs-R-Us Lines: 58