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!

counting atoms/ elements in list (new at prolog)

Status
Not open for further replies.

johanprolog

Programmer
Mar 13, 2007
2
ZA
% to get elements in list
mymember(X,[X|_]).
mymember(X,[_|T]) :- mymember(X , T).

?- mymember(X,[a,b,[1,2,3],c]).

X = a ;

X = b ;

X = [1, 2, 3] ;

X = c ;

%to get atoms in list.
mymember(X,[X|_]).
mymember(X,[_|T]) :- mymember(X , T).
mymember(X,[H|_]) :- mymember(X , H).

?- mymember(X,[a,b,[1,2,3],c]).

X = a ;

X = b ;

X = [1, 2, 3] ;

X = c ;

X = 1 ;

X = 2 ;

X = 3 ;

i need to count the elements and atoms what do I need to add
to the code to get the count?

Thnx Johan
 
got the element part right but still not sure about the atom part.

len([],0).
len([_|T],N) :- len(T,M),N is M+1.
?- len([[1,2],[3,4],4,5]).
X = 3;

Atoms should be X = 6;
any help or where can I look on the web?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top