Re: Prolog Help



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
.



Relevant Pages

  • Re: Calculating distances in O(1)
    ... >> Find the total distance between any two cities if: ... >> source city destination city distance ... Create an array of distances from city 0. ...
    (comp.programming)
  • Re: Calculating distances in O(1)
    ... > Find the total distance between any two cities if: ... the distance between Atlanta to Norfolk would be 2770 ... look up the two positions and perform the distance calculation. ... be able to look up city names which can easily be done in Otime ...
    (comp.lang.c)
  • Prolog Help
    ... I am just new to prolog, and declrative languages in general, so I am ... finding difficulties expression the facts and rules in prolog. ... there's at least one distance from city A, ...
    (comp.lang.prolog)
  • Travelling Salesman with Spherical Coordinates.
    ... come up with a list of cities which he thinks will provide the best market ... furthermore, he figures the less distance he has to travel, the cheaper his ... you will be given the radius of the planet that this ... Parameters: String, int ...
    (comp.lang.java.programmer)
  • Re: Floyd Warshall
    ... tells me the distance. ... "crea una tabella n*n con le distanze piu brevi fra le citta" ... (dolist (c1 cities) ... ;;; bijective map between cities and indices ...
    (comp.lang.lisp)