Re: Prolog Help
- From: Chip Eastham <hardmath@xxxxxxxxx>
- Date: Sat, 3 May 2008 07:09:55 -0700 (PDT)
On May 3, 4:16 am, Hatem <h.m.serg...@xxxxxxxxx> wrote:
Hi all,
I am just new to prolog, and declrative languages in general, so I am
finding difficulties expression the facts and rules in prolog. I have
tried to solve the following exercise, but it seems that something is
missing.
The problem is to write a predicate distanceFrom_to/2 which represents
the distance between to cities. Durning the test I should make sure
that the following quries hold:
- there's at least one distance from city A, to city B.
- there's a distance from city a to city b.
- there's no distance from one city to itself.
What I have made is the following:
connectedWith(berlin,hamburg).
connectedWith(hamburg,duesseldorf).
connectedWith(duesseldorf,stuttgart).
connectedWith(stuttgart,muenchen).
connectedWith_2(S1,S2) :- connectedWith(S1,S2).
connectedWith_2(S1,S2) :- connectedWith(S2,S1).
distanceFrom_to(S1,S2):- connectedWith_2(S1,S2).
distanceFrom_to(S1,S2):-
connectedWith_2(S1,S3),connectedWith_2(S3,S2).
and in fact, this program does not do what I am supposed, and I failed
to define the constraints above. Could any body help me? I would be
nice if some one can also suggest some resource to benifit from wiht
regard to this problem.
Thanks
Mossalli
Without knowing more about the context, I have two
guesses:
1. Perhaps you've made the problem harder than it
was supposed to be. You might have been expected
just to articulate some "fact" clauses for the
predicate distanceFrom_To which assign distances
to various pairs of distinct cities.
2. If there is a deeper problem here, then it may
be to derive a "shortest path" from a starting set
of distances between cities. Search the Web for
Dijkstra's shortest path algorithm to see what may
be involved in such derivations.
regards, chip
.
- References:
- Prolog Help
- From: Hatem
- Prolog Help
- Prev by Date: Prolog Help
- Next by Date: Re: jpl and segmentation fault
- Previous by thread: Prolog Help
- Index(es):
Relevant Pages
|