Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to check whether a given list is palindrome or not

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

Prolog program to check whether a given list is palindrome or not.

Code for Prolog program to check whether a given list is palindrome or not in Artificial Intelligence

domains

    list=symbol*

    
predicates

    palin(list)
    findrev(list,list,list)
    compare(list,list)

    
clauses

    palin(List1):-
        findrev(List1,[],List2),
        compare(List1,List2).
            
    findrev([],List1,List1).
    
    findrev([X|Tail],List1,List2):-
        findrev(Tail,[X|List1],List2).
        
    compare([],[]):-
        write("\nList is Palindrome").
        
    compare([X|List1],[X|List2]):-
        compare(List1,List2).    
        
    compare([X|List1],[Y|List2]):-
        write("\nList is not Palindrome").
        

OUT PUT
=======

Goal: palin([y,o,g,e,s,h])

List is not Palindrome

Yes

--------------------------

Goal: palin([n,a,y,a,n])

List is Palindrome

Yes
  
Share: 



Milind Mishra
Milind Mishra author of Prolog program to check whether a given list is palindrome or not 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!