Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, I am very new to Prolog, and I am having a terrible time getting to grips with it. If someone coukld please help with this problem I would be extremely grateful. I think it is reletively simple, so hopefully someone can explain to me what is going wrong. Basically, this program is meant to find routes between these places. At the moment it just needs to check that it is not re-visiting the same location twice. In the first journey rule I am trying to check that the destination has not already been visited. In the second journey rule I am trying to use an accumulator in the form of the list Beenthere, where it adds each location to the head of the list after it has been visited. The trip rules are fine, but I think there are a number of problems with my journey rules. Can someone please tell me how to get this program to (a) check that it is not re-visiting any places and (b) add visited places to the list? ------------------------------------------------------ % wessex2.pl :-use_module(library(lists)). % road/3 road(budmouth, casterbridge, 5). road(casterbridge, exonbury, 30). road(casterbridge, glaston, 30). road(casterbridge, idmouth, 25). road(downstaple, exonbury, 20). road(downstaple, glaston, 40). road(downstaple, tivworthy, 15). road(glaston, tivworthy, 30). road(exonbury, idmouth, 10). road(exonbury, tivworthy, 10). % rule 1 trip(Place1, Place2, Dist) :- road(Place1, Place2, Dist). % rule 2 trip(Place1, Place2, Dist) :- road(Place2, Place1, Dist). % part 2.2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%% % rule 1 journey(Place1, Place2, Dist, Route, Beenthere) :- trip(Place1, Place2, Dist), Route = [Place1, Place2], \+ (member(Place2, Beenthere)). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%% % rule 2 journey(Place1, Place2, Dist, Route, Beenthere) :- journey(Place1, PLACE3, ADIST, Route1,Beenthere1), journey(PLACE3, Place2, BDIST, Route2,Beenthere1), Dist is +(ADIST, BDIST), append([Place1], Route2, Route), Beenthere = [Head|Tail], \+ (member(PLACE3, Beenthere)), Beenthere1 = [Place1|Beenthere]. ----------------------------------------------------------- Oh, and the \ symbols are actually backslashes, but for some reason they are not appearing correctly on this pc. Any help would be greatly appreciated. Thanks very much.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.