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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

predicate that returns a list of positions of given Subterm

Status
Not open for further replies.

LordHelmet666

Programmer
Jul 15, 2008
1
US
Can someone help me with this?


I have a predicate that returns to me the number of times
the Subterm occurs, but I need it to return to me a list of
the positions it occurs at.


*******************WHAT I HAVE*******************
position(_,[],0).
position(X,[X|L],N) :- position(X,L,M), N is M+1.
position(X,[Y|L],N) :- not(X=Y), position(X,L,N).
*************************************************
?-position(a, [a,b,c,a,d,a,e,a,f], P).

P=4
*************************************************



=================WHAT I NEED=====================
CODE FOR THE FUNCTION TO RETURN LIST OF POSITIONS
=================================================
?-position(a, [a,b,c,a,d,a,e,a,f], P).

%Need the code for this version


%This is what it is supposed to return.

P = [1,4,6,8]
=================================================


Any help would be extremely appreciated.

Thanks

-LordHelmet666






 
*******************WHAT I HAVE*******************
position(_,[],0).
position(X,[X|L],N) :- position(X,L,M), N is M+1.
position(X,[Y|L],N) :- not(X=Y), position(X,L,N).
*************************************************
To do what you want, you need un argument which count the number of the loop. Every time the comparaison succeeds, you memorize it in a list.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top