×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# building a predicate 'is_top'

## building a predicate 'is_top'

(OP)
Hi,
how can i build a predicate to find the top card of a deck of cards. e.g

card(desk,a).
card(a,b).
card(b,c).
card(c,d).
card(d,e).
--------------------------
?- is_top(T).
T = e
Yes
?- is_top(d).
No

### RE: building a predicate 'is_top'

At first glance, you can fetch all the Y of card(X,Y) and top is the last of the list.

### RE: building a predicate 'is_top'

hi

create a list with all the elements.
reverte the list and the top is the on the head

I don´t know if you can use predicates pre defined like "findall, reverse,head"

Ricardo
:)

### RE: building a predicate 'is_top'

Hi

no need to of complicate.
You need put all in to a list. and write the predicate LAST

nn987

### RE: building a predicate 'is_top'

%first put into a list
lstCard(C):-findall(Y,card(_,Y),C).

%the last element of the list
lastb([]):-!,fail.
lastb([E],E):-!.
lastb([_|T],L):-lastb(T,L).

%is_top(e).
is_top(L):-lstCard(C),lastb(C,L).

### RE: building a predicate 'is_top'

There is another way with assert/retract :

#### CODE

is_top(X) :-
forall(card(_, Y), (retractall(a(_Y)), assert(a(Y)))),
retract(a(X)).

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!