Prolog program for finding the sum of all numbers in a given list in Artificial Intelligence


 % Prolog program for find the sum of all numbers in a given list-------
% Alamgir Hossain, CSE, JUST

% Call the rules like list([1,2,3,9,4,4,5],Result).

list([H|T],Result):-

    listhelper(T,H,Result).
    listhelper([],Acc,Acc).
listhelper([H|T],Acc,Result):-

    Nacc is H+Acc,

    listhelper(T,Nacc,Result).
% End of code
  
  
% Go to consol and check the output--------

%  list([1,2,3,9,4,4,5],Result).

% Result = 28

No comments:

Post a Comment