Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to find the nth element of a list

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

Prolog program to find the nth element of a list.

Code for Prolog program to find the nth element of a list in Artificial Intelligence

domains
    x = integer
    l = integer*
    
predicates
    find(l,x)
    
clauses

    find([],N) :-
        write("There is no such element in the list"),nl.
        
    find([Element|List],1) :-
        write("The element is ",Element),nl.
        
    find([Element|List],N) :-
        N1 = N-1,
        find(List,N1).
        
Output :

Goal: find([1,2,3,4],3)
The element is 3
Yes

Goal: find([1,2,3,4],0)
There is no such element in the list
Yes

Goal: find([1,2,3,4],5)
There is no such element in the list
Yes

Goal: find([1,2,4,3],4)
The element is 3
Yes
  
Share: 


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

Milind Mishra
Milind Mishra author of Prolog program to find the nth element of a 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!