For Programmers: Free Programming Magazines  


Home > Archive > Prolog > April 2004 > User interface I/O









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 User interface I/O
bex145

2004-04-27, 10:04 am

Hi i am writing a route calculator program in prolog and i am having
difficulty with the user interface.

i need to query the user where they wish to go. i can get that to work and
read in the input and then use it in a predicate but when i want to use
that input again in another predicate it is lost and does not have
anything assigned to it. I also want to check that the input is correct
that the destination exists and i have done that a long way around i
think?

I also get errors when consulting saying i have singleton variables. has
anyone got any suggestions on my code for a suitable user interfaces how
to do them properly- where you can check input and use the input terms
again or assign them. i have been trying to get this to work for ws so
any help would be much appreciated.

excert from my code:

:- write('Welcome to the Tripfinder'),
tripfinder.

tripfinder:-
nl,
write('Where are you traveling from?'),
nl,
read(A),
try(A).

again:-
write('Where are you going to?'),
nl,
read(B),
process(A,B).

try(london):-
again.
try(guilford):-
again.
try(downtown):-
again.
try(exworth):-
again.
try(glastoby):-
again.
try(dabton):-
again.
try(tinville):-
again.
try(Other):-
write('Sorry your destination was not recognised'),
nl,
write('please try again.'),
tripfinder.

process(A,B):-journey(A,B,DIST,ROUTE),
write('The route found is'), write(ROUTE),
nl,
write('The distance is '), write(DIST), write(' miles'),nl,
write('Would you like a list of all possible routes?'),nl,
read(Z),
check(Z),
tripfinder.

printlist([First|Rest]):-
N is 1,
write(N),
write(' Dist Miles: '),
write(First),
N is N + 1,
printlist(Rest),
nl.

check(no):-
!.

check(yes):- findall((DIST,ROUTE),journey(A,B,DIST,RO
UTE),List),
printlist(List),
nl,
write(DIST),
nl,
write(ROUTE).

check(Z):-
write('please respond yes or no'),
nl,
read(Y),
check(Y).

:-use_module(library(lists)).

road(london,guilford,5).
road(guilford,exworth,30).
road(guilford,glastoby,30).
road(guilford,dabton,25).
road(downtown,exworth,20).
road(downtown,glastoby,40).
road(downtown,tinville,15).
road(glastoby,tinville,30).
road(exworth,dabton,10).
road(exworth,tinville,10).

trip(A,B,DIST):-
road(A,B,DIST).
trip(A,B,DIST):-
road(B,A,DIST).

journey(A,B,DIST,ROUTE):-
journey(A,B,DIST,ROUTE,[]).

journey(A,B,DIST,ROUTE,BEENTHERE):-
trip(A,B,DIST),
\+ member(B,BEENTHERE),
ROUTE = [A,B].

journey(A,B,DIST,ROUTE,BEENTHERE):-
trip(A,PLACE,ADIST),
\+ member(PLACE,BEENTHERE),
journey(PLACE,B,BDIST,VISITED,[A|BEENTHERE]),
ROUTE = [A|VISITED],
DIST is ADIST + BDIST.


also i need to work out the shortest route and i can do that using the </1
predicate but i am unsure as to how i can compare two DIST values in two
results- how to check and store them. any help will be greatly
appreciated.

cheers

Sponsored Links







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

Copyright 2008 codecomments.com