To count the number of elements in a list in PROLOG. Earthcare Foundation NGO 22:14 Prolog No comments list([],0). list([H|T],C):-list(T,Y),C is Y+1. Output 1 ?- list([1,2,3,5],C). C = 4. 2 ?- list([1,2,3,5,4,5,6,7],C). C = 8. 3 ?- list([],C). C = 0. Share This: Facebook Twitter Google+ Stumble Digg Email ThisBlogThis!Share to XShare to Facebook Related Posts:To find the largest of three numbers in PROLOG. max(P,Q,R):-P>Q,P>R,write('Larger number is '),write(P). max(P,Q,R):-P<Q,Q>R,write('Larger number is '),write(Q). max(P,Q,R):-R>Q,P&l… Read MoreTo find the largest of three numbers in PROLOG. max(P,Q,R):-P>Q,P>R,write('Larger number is '),write(P). max(P,Q,R):-P<Q,Q>R,write('Larger number is '),write(Q). max(P,Q,R):-R>Q,P&l… Read MoreTo find the factorial of given number in PROLOG. fact(X,Y):-X is 0,Y is 1; X>0,N is X-1, fact(N,G),Y is X*G. Output 1 ?- fact(3,X). X = 6 . 2 ?- fact(22,X). X = 1124000727777607680… Read MoreTo find the GCD of two numbers in PROLOG. gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X); X=0,write('GCD of two numbers is '),write(Y); Y=0,write('GCD of two numbers is '),write(X)… Read MoreTo find the larger of two numbers in PROLOG. max(P,Q):-P>Q,write('Larger number is '),write(P). max(P,Q):-P<Q,write('Larger number is '),write(Q). max(P,Q):-P=Q,write('Both are equal '). … Read More
0 comments:
Post a Comment