Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

building a predicate 'is_top'

Status
Not open for further replies.

waqas246

Programmer
Feb 17, 2010
2
0
0
GB
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
 
At first glance, you can fetch all the Y of card(X,Y) and top is the last of the list.
 
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
:)
 
Hi

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

nn987
 
%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).
 
There is another way with assert/retract :
Code:
is_top(X) :-
	forall(card(_, Y), (retractall(a(_Y)), assert(a(Y)))),
	retract(a(X)).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top