For Programmers: Free Programming Magazines  


Home > Archive > Prolog > March 2004 > Help with basic list functions









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help with basic list functions
Scaramanga

2004-03-27, 12:11 am

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.
















Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com