/*Q-13:Write a Prolog program to implement two predicates evenlength(List) and
oddlength(List) so that they are true if their argument is a list of even or odd length
respectively.*/
evelen([]).
evelen([|[|List]]):-evelen(List).
oddlen([_]).
oddlen([|[|List]]):-oddlen(List).
/*Q14: Write a Prolog program to implement nth_element (N, L, X) where N is the desired
position, L is a list and X represents the Nth element of L*/
nth_element(1,[H,T],H).
nth_element(N,[H,T],X):- N1 is N-1,nth_element(N1,T,X).
/*Q15: Write a Prolog program to implement maxlist(L, M) so that M is the maximum
number in the list.*/
max_element([H],H).
max_element([H|T],M):-max_element(T,M1),H>=M1, M is H,!.
max_element([H|T],M):-max_element(T,M1),H<M1, M is M1.
/*Q16: Write a prolog program to implement insert_nth (I, N, L, R) that inserts an item I into
Nth position of list L to generate a list R.*/
insert(L,[X|Y],[L|_]).
insert(L,Pos,[X|Y],[X|M]):-Pos>1,Pos1 is Pos-1,insert(L,Pos1,Y,M).
insert(L,1,[X|Y],M):- append([L],[X|Y],M).
/*Q17: Write a Prolog program to implement delete_nth (N, L, R) that removes the element
on Nth position from a list L to generate a list R.*/
delete_nth(1,[_|L],L).
delete_nth(Pos,[H|L],[H|R]):-Pos1 is Pos-1, delete_nth(Pos1,L,R).
/*Q18: Write a program in PROLOG to implement merge (L1, L2, L3) where L1 is first
ordered list and L2 is second ordered list and L3 represents the merged list.*/
merge([],[],[]).
merge([],L2,L2).
merge(L1,[],L1).
merge([H1|T1],[H2|T2],[H1|T3]):- H1=<H2,merge(T1, [H2|T2], T3).
merge([H1|T1],[H2|T2],[H2|T3]):- merge([H1|T1], T2, T3).