To find the sum of given list in PROLOG Earthcare Foundation NGO 22:22 Prolog No comments list_sum([],0). list_sum([H|T],Total):-list_sum(T,Sum1),Total is H+Sum1. Output 1 ?- list_sum([1,2,34,4],Sum). Sum = 41. 2 ?- list_sum([1,2,34,4,3,6,7,8],Sum). Sum = 65. Share This: Facebook Twitter Google+ Stumble Digg Email ThisBlogThis!Share to XShare to Facebook Related Posts:To 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 perform arithmetic operations in PROLOG sum(X,Y):-Z is X+Y, write('Sum of two numbers is '),write(Z). minus(X,Y):-X>Y,Z is X-Y, write('Subtraction of two numbers is '),write(Z); X&… 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 construct a binary tree in PROLOG root(a). child(b,a). child(c,a). child(d,b). child(e,b). child(f,c). child(g,c). predecessor(X,Y):-child(Y,X). successor(X,Y):-child(X,Y). parent(X,… Read MoreTo count the number of elements in a list in PROLOG. 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 … Read More
0 comments:
Post a Comment