Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to concatenate two lists, an element is a member of a given list or not, reverse a list and delete a list

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

Program that performs the following functions
1 : Concatenates two lists into one
2 : Check whether an element is a member of a list or not
3 : Reverse a list
4 : Delete an element from a list

Code for Prolog program to concatenate two lists, an element is a member of a given list or not, reverse a list and delete a list in Artificial Intelligence

domains
    x = integer
    l = integer*
    
predicates
    member(x,l)
    is_mem(x,l)
    concatenate(l,l,l)
    delete_first2(l,l)
    delete_last3(l,l)
    delete_ele(x,l)
    reverse_list(l,l)
    reverse(l,l,l)
    
clauses
    member(Element,[Element|List]).
    
    member(Element,[_|List]) :- 
        member(Element,List).
    
    is_mem(Element,List) :-
        member(Element,List),!,
        write(Element," is a member of list ",List),nl.
        
    is_mem(Element,List) :-
        write(Element," is not a member of list " ,List),nl.
    
    concatenate([],List,List).
    
    concatenate([X|List1],List2,[X|List3]) :-
        concatenate(List1,List2,List3).
 
     delete_ele(Element,List) :-
         concatenate(List1,[X|List2],List),
         concatenate(List1,List2,ListNew),
         write("The new List is ",ListNew),nl.
         
     delete_ele(Element,List) :-
         write(Element , " not found in list\n").
         
    delete_first2(List,NewList) :-
        concatenate([_,_],NewList,List).
    
    delete_last3(List,NewList) :-
        concatenate(NewList,[_,_,_],List).
        
    reverse_list(IN,OUT) :-
        reverse(IN,[],OUT).
    
    reverse([],IN,IN).
    
    reverse([Head|Tail],L1,L2) :-
        reverse(Tail,[Head|L1],L2).    


Output :

For concatenating two list into one :

Goal: concatenate([1,3,4],[2,5,7],List)
List=[1,3,4,2,5,7]
1 Solution

For deletion : 

Goal: delete_ele(2,[1,3,2,4,5])
The new List is [1,3,4,5]
Yes

Goal: delete_ele(2,[1,3,4,5,6])
2 not found in list
Yes

For reverse :

Goal: reverse_list([1,2,3,4,5],List)
List=[5,4,3,2,1]
1 Solution

For finding a member :

Goal: is_mem(2,[1,2,3,4])
2 is a member of list [1,2,3,4]
Yes

Goal: is_mem(2,[1,3,4,5])
2 is not a member of list [1,3,4,5]
Yes

For deleting first two elements of a list :

Goal: delete([1,2,3,4],L)
L=[3,4]
1 Solution

For deleting last three elements of a list :

Goal: delete_last3([1,2,3,4,5],L)
L=[1,2]
1 Solution
  
Share: 



Milind Mishra
Milind Mishra author of Prolog program to concatenate two lists, an element is a member of a given list or not, reverse a list and delete a list is from India.
 
View All Articles

Related Articles and Code:


 
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!