Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program that defines a relation count(A,L,N) that counts into N the number of occurrences of the element A in the list L

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

Prolog program that defines a relation count(A,L,N) that counts into N the number of occurrences of the element A in the list L.

Code for Prolog program that defines a relation count(A,L,N) that counts into N the number of occurrences of the element A in the list L in Artificial Intelligence

trace
domains
    x = integer
    l = integer*
    
predicates
    count(x,l,x)
    
clauses
    count(Element,[],0).
    
    count(Element,[X|List],OccurNum) :-
        Element = X,!,
        count(Element,List,OccurNum1),
        OccurNum = OccurNum1 + 1.
        
    count(Element,[X|List],OccurNum) :-
        count(Element,List,OccurNum).
        
Output :

Goal: count(2,[2,3,2,5,2,6],N)

N=3
1 Solution

Goal: count(2,[],N)

N=0
1 Solution
  
Share: 



Milind Mishra
Milind Mishra author of Prolog program that defines a relation count(A,L,N) that counts into N the number of occurrences of the element A in the list L is from India.
 
View All Articles

Related Articles and Code:


 
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!