Feb 20, 2010 #1 waqas246 Programmer Feb 17, 2010 2 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
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
Feb 20, 2010 #2 joel76 Programmer Aug 31, 2008 295 FR At first glance, you can fetch all the Y of card(X,Y) and top is the last of the list. Upvote 0 Downvote
Mar 8, 2010 #3 nn987 Programmer Feb 4, 2006 37 GB 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 Upvote 0 Downvote
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
Mar 8, 2010 #4 nn987 Programmer Feb 4, 2006 37 GB Hi no need to of complicate. You need put all in to a list. and write the predicate LAST nn987 Upvote 0 Downvote
Mar 9, 2010 #5 nn987 Programmer Feb 4, 2006 37 GB %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). Upvote 0 Downvote
%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).
Mar 11, 2010 #6 joel76 Programmer Aug 31, 2008 295 FR There is another way with assert/retract : Code: is_top(X) :- forall(card(_, Y), (retractall(a(_Y)), assert(a(Y)))), retract(a(X)). Upvote 0 Downvote
There is another way with assert/retract : Code: is_top(X) :- forall(card(_, Y), (retractall(a(_Y)), assert(a(Y)))), retract(a(X)).