- #1
Potatochip911
- 318
- 3
Homework Statement
Write a predicate to determine if two cards are in the same pile. The placement of the cards is given as facts above(x,y), x is above y, or below(x,y), x is below y. I'm supposed to do this using Prolog which is a first-order logic language.
Homework Equations
The Attempt at a Solution
I made an arbitrary pile of cards to test my method, the facts for this pile I'm using are.
Code:
below(c,b).
below(d,c).
above(a,b).
above(p,a).
Code:
samePile(X, Y) :- above(X, Y), !.
samePile(X, Y) :- above(Y, X), !.
samePile(X, Y) :- below(X, Y), !.
samePile(X, Y) :- below(Y, X), !.
samePile(X, Y) :- above(X, Z), samePile(Z, Y), !; above(Z, X), samePile(Z, Y), !.
samePile(X, Y) :- below(Z, X), samePile(Z, Y), !; below(X, Z), samePile(Z, Y), !.
One example query that results in an infinite loop is samePile(p,d) which will alternate between calling samePile(a,d) and samePile(b, d) since a is stored as above(a, b) so whenever we get to samePile(b, d) we will just go back up to samePile(a,d). I am completely lost at this point as to how I can make a recursion that travels through the entire pile.