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