0

Given the code below, what is the ToConnection argument? It seems to have no connection to any other arguments and is created seemingly arbitrarily. Every other argument can be traced back to its origins when the predicate is called with given values like routing(chicago,newyork,X). ignoring X of course!

So FromCity begins as chicago and continues from there being assigned new values, same for ToCity. They have specific values in the beginning.

Like with ToConnection, X seems to also be left undefined and unreferenced. It seems like they're supposed to take up any values that match the predicates they're part of!? So ToConnection in the path() predicate becomes all the other cities that are related to FromCity=chicargo, in other words, it gets all the second values from the path() predicates that match this pattern - path(chicargo, ???). Resulting in picking these predicates

path(chicago,atlanta). path(chicago,milwaukee). path(chicago,detroit).

Overall, I'd like to understand them better as they seem to be called in isolation and I'm having a hard time tracing them back to something tangible to reference what they're connected to. They are hard to understand because they don't have any obvious relation to anything at first sight!

path(chicago,atlanta).
path(chicago,milwaukee).
path(milwaukee,detroit).
path(milwaukee,newyork).
path(chicago,detroit).
path(detroit, newyork).
path(newyork, boston).
path(atlanta,boston).
path(atlanta, milwaukee).

routing(FromCity, ToCity, [FromCity, ToCity]) :-
  path(FromCity, ToCity).

routing(FromCity, ToCity, [FromCity|Connections]) :- 
  path(FromCity, ToConnection),
  routing(ToConnection, ToCity, Connections).

Code from https://stackoverflow.com/a/13172689

false
  • 10,264
  • 13
  • 101
  • 209
C9C
  • 319
  • 3
  • 14
  • Please note that calling a direct connection a `path/2` is not very helpful [as also this answer suggests](https://stackoverflow.com/a/34077725/772868), because it goes against common terminology. Call it `edge` `directconnection` whatever. A [path](https://en.wikipedia.org/wiki/Path_(graph_theory)) is rather a sequence of such edged which is called here `routing`. – false Aug 07 '21 at 15:58

1 Answers1

0

There are two uses of the ToConnection variable. That's perfectly normal.

routing(FromCity, ToCity, [FromCity|Connections]) :-
    path(FromCity, ***ToConnection***),
    routing(***ToConnection***, ToCity, Connections).
Enigmativity
  • 113,464
  • 11
  • 89
  • 172