Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to define the relation last(item,list) so that item is the last element of the list without the use of concatenate

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

Prolog program to define the relation last(item,list) so that item is the last element of the list without the use of concatenate.

Code for Prolog program to define the relation last(item,list) so that item is the last element of the list without the use of concatenate in Artificial Intelligence

domains
    x = integer
    l = integer*
    
predicates
    last(X,L)
    list_add(l,x,l)
    
clauses
    list_add([],X,[X]).

    list_add([Y|List1],X,[Y|List2]) :-
        list_add(List1,X,List2).
        
    last(X,List) :-
        list_add(List,X,List1),
        write("The new list is ",List1),nl.
        
        
Output :

Goal: last(3,[3,6,1,5])

The new list is [3,6,1,5,3]
Yes
  
Share: 



Milind Mishra
Milind Mishra author of Prolog program to define the relation last(item,list) so that item is the last element of the list without the use of concatenate 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!