Hello,

I am writing a prolog program that compares the positions of buildings.

The only fact I can use is west(building1, building2), which says that building 1 is immediately west of building 2. I have declared many of these for the two rows of buildings I am using.

I am having trouble with a rule called nextdoor(X,Y). Obviously, when I just put the west fact into the rule and test it with two buildings X Y, when X is west of Y, the rule comes out to true. But when I switch the assignment of buildings to the variables it doesn't work.

For example, if building 2 is immediately east of building 1, they are still next door, but I can not figure out how to make this come out to true using only the west(X, Y) fact.

I am only allowed to use the west facts, nothing else.

I'm assuming I need to add some "ands" in the rule and use another variable but I can not figure out how to structure it for life of me!

Any help would be greatly appreciated.

Thank you!

Recommended Answers

All 2 Replies

Wouldn't something like nextdoor(X,Y) :- not west(X,Y) == west(Y,X) work?

If X is west of Y you get No == No, which is true.
If Y is west of X you get Yes == Yes, which is true.
If neither is west of the other you get Yes == No which is false (so they aren't next)
If both are west of eachother (impossible!) you also get false.

If building X is east of building Y it means that building Y is to the west of building X. nextdoor(X,Y) :- west(X,Y) ; west(Y,X).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.