Search:

Submit Article

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

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

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 author of Prolog program to find whether the length of a list is even or odd is from India. View All Articles

 Please enter your CommentComment should be atleast 30 Characters.Please put code inside [Code] your code [/Code]. No Comment Found, Be the First to post comment!