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

  • Thread starter Potatochip911
  • Start date
In summary, the conversation discusses how to write a predicate in Prolog to determine if two cards are in the same pile, using the given facts of above(x,y) and below(x,y). The conversation also mentions running into infinite loops while trying to write the recursive predicate and suggests starting from one card and going in both directions to find the other card. Finally, it is noted that it may be necessary to include both above and below facts in the pile representation.
  • #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).
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
  • #2
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 Potatochip911
  • #3
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.
 

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

1. How can I determine if a card is in the same pile as another card?

To determine if a card is in the same pile as another card, you can compare their positions in the pile. If the two cards are stacked on top of each other or are adjacent to each other, then they are in the same pile. Otherwise, they are in different piles.

2. What factors should I consider when determining if a card is in the same pile as another card?

When determining if a card is in the same pile as another card, you should consider the position of the cards in the pile, the order of the cards, and any overlapping or gaps between the cards. These factors can help you determine if the cards are in the same pile or not.

3. Can I use any specific techniques to determine if a card is in the same pile as another card?

Yes, there are several techniques that can be used to determine if a card is in the same pile as another card. These include visually inspecting the pile, using mathematical calculations to determine the position of the cards, or using a computer program to analyze the pile.

4. How can I ensure that my method of determining if a card is in the same pile as another card is accurate?

The best way to ensure accuracy when determining if a card is in the same pile as another card is to double check your results. You can do this by comparing your method to other established methods or by having someone else independently verify your results.

5. Are there any common mistakes to avoid when determining if a card is in the same pile as another card?

Some common mistakes to avoid when determining if a card is in the same pile as another card include not properly considering the position and order of the cards, not accounting for any overlapping or gaps between the cards, and using an unreliable or untested method. It is important to carefully consider all factors and to use a proven and accurate method for the best results.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
3
Views
394
  • Introductory Physics Homework Help
Replies
25
Views
443
  • Calculus and Beyond Homework Help
Replies
1
Views
564
  • Engineering and Comp Sci Homework Help
Replies
3
Views
771
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Precalculus Mathematics Homework Help
Replies
5
Views
1K
  • Special and General Relativity
Replies
16
Views
1K
Replies
40
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
814
Back
Top