1
votes

I've started learning Prolog recently and came across a problem.

Some people sit around a table. We are given the fact sits_right_of(X, Y) (X sits right of Y). Then I wrote the next rules:

  • sits_left_of(X, Y) :- sits_right_of(Y, X) (X sits left of Y);
  • are_neighbors_of(X, Y, Z) :- sits_left_of(X, Z) (X sits left to Z and Y sits right to Z);
  • next_to_each_other(X, Y) :- are_neighbors_of(_, X, Y); are_neighbors_of(X, _, Y) (X sits next to Y).

How can I find out who is the person who sits two places right (or left) to a given person? Is there some kind of recursive query like sits_right_of(sits_right_of(X, alex))? Do I need to write another rule for finding out who sits n places away from somebody?

1

1 Answers

0
votes

You obtain values from a procedure the same way you enter them. Introduce a new variable and try to satisfy sits_right_of(Y, alex). Then, this must also be satisfied: sits_right_of(X, Y). The procedure can be defined like this:

sits_two_places_right_of(X, Y) :- sits_right_of(X, Z), sits_right_of(Z, Y).

Yes, such a procedure can be created analogically to this for every number or persons, or using arithmetics, if you want to make it a parameter.