Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to find whether the length of a list is even or odd

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

Prolog program to find whether the length of a list is even or odd.

Code for Prolog program to find whether the length of a list is even or odd in Artificial Intelligence

domains
    x = integer
    l = integer*
    
predicates
    length(l,x)
    evenlength(l)
    oddlength(l)
    
clauses
    length([],0).
    
    length([X|List],Length) :-
        length(List,Length1),
        Length = Length1 + 1.
        
    evenlength(List) :-
        length(List,Length),
        Length mod 2 = 0.
    
    oddlength(List) :-
        length(List,Length),
        Length mod 2 <> 0.
        
Output :

For evenlength :

Goal: evenlength([1,2,3,4])
Yes

Goal: evenlength([1,2,3,4,5])
No

For oddlength :

Goal: oddlength([1,2,3,4,5])
Yes

Goal: oddlength([1,2,3,4])
No
  
Share: 



Milind Mishra
Milind Mishra author of Prolog program to find whether the length of a list is even or odd is from India.
 
View All Articles

 

Other Interesting Articles in Artificial Intelligence:


 
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!