Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to find the sum of the elements in the list

Posted By: Milind Mishra     Category: Artificial Intelligence     Views: 6765

Prolog program to find the sum of the elements in the list.

Code for Prolog program to find the sum of the elements in the list in Artificial Intelligence

domains
    x = integer
    l = integer*
    
predicates
    sum(l,x)
    
clauses
    sum([],0).
    
    sum([X|List],Sum) :-
        sum(List,Sum1),
        Sum = X + Sum1.
        
Output :

Goal: sum([1,2,3,4],Sum)
Sum=10
1 Solution

Goal: sum([-2,-1,1,2],Sum)
Sum=0
1 Solution

Goal: sum([],Sum)
Sum=0
1 Solution

Goal: sum([1],Sum)
Sum=1
1 Solution
  
Share: 


Didn't find what you were looking for? Find more on Prolog program to find the sum of the elements in the list Or get search suggestion and latest updates.

Milind Mishra
Milind Mishra author of Prolog program to find the sum of the elements in the list is from India.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!