Determining if a card is in the same pile as another card

  • Thread starter Thread starter Potatochip911
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on creating a Prolog predicate, samePile(X, Y), to determine if two cards are in the same pile based on their relative positions defined by the predicates above(x,y) and below(x,y). The user encounters infinite loops when attempting to implement recursion to traverse the pile. The solution involves refining the recursion to ensure it explores both directions without redundancy, as the relationships above(a,b) and below(b,a) are equivalent but should be stored only once to avoid confusion.

PREREQUISITES
  • Understanding of Prolog syntax and predicates
  • Knowledge of first-order logic concepts
  • Familiarity with recursion in programming
  • Basic concepts of data structures, specifically stacks
NEXT STEPS
  • Refine the samePile(X, Y) predicate to prevent infinite loops
  • Explore Prolog's cut operator (!) and its implications on backtracking
  • Investigate alternative data structures for representing card piles
  • Learn about Prolog's built-in predicates for list manipulation to enhance recursion
USEFUL FOR

Prolog developers, computer science students studying logic programming, and anyone interested in implementing recursive algorithms in Prolog.

Potatochip911
Messages
317
Reaction score
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).
So from top to bottom my pile is p->a->b->c->d. The problem I'm running into when writing the recursive samePile(X, Y) predicate is I keep running into infinite loops. I'm not sure how to write the recursion so that it will exhaust all options going up the pile and then exhaust all options going down the pile. This is the code I currently have:

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.
 
Physics news on Phys.org
above(a,b) == below(b,a)? If yes, get rid of one.
You can just start from card A and go in both directions as far as I see.
 
  • Like
Likes   Reactions: Potatochip911
mfb said:
above(a,b) == below(b,a)? If yes, get rid of one.
You can just start from card A and go in both directions as far as I see.

While the two statements are equivalent only one will be stored as a fact. A pile could be expressed as a bunch of above(x,y) facts, under(x,y) facts, or a combination of both so it seems necessary to include both.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 0 ·
Replies
0
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
938
  • · Replies 13 ·
Replies
13
Views
2K
Replies
3
Views
2K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K