Monday, July 17, 2023

8. Write a Prolog program to implement append for two lists.

  8. Write a Prolog program to implement append for two lists.

app([],L,L).

app([X|M],N,[X|Q]):-

 app(M,N,Q).



OUTPUT:

app([1,2],[3,4],X).

X = [1, 2, 3, 4]

app([1,2],[],X).

X = [1, 2]

 


7. Write a Prolog program, remove-nth(Before, After) that asserts the After list is the Before list with the removal of every n‘th item from every list at all levels.

 7. Write a Prolog program, remove-nth(Before, After) that asserts the After list is the Before list with the removal of every n‘th item from every list at all levels.


del(1,[_|T],T).

del(P,[X|Y],[X|R]):-

 P1 is P-1,

 del(P1,Y,R).



dalpos(P,L,R):-

 P1 is P-1,

 del(P1,L,R1),

 del(P,R1,R).


OUTPUT:


del(2,[10,20,30,40],X).

X = [10, 30, 40]

Next101001,000 Stop


9. Write a Prolog program to implement palindrome (List).

 9. Write a Prolog program to implement palindrome (List). 


pal([]):- write('palindrome').

pal([_]):- write('palindrome').

pal(L) :-

 append([H|T], [H], L),

 pal(T) ;write('Not a palindrome').


OUTPUT:

pal([1,2,1]).

palindrome

1true

Next101001,000 Stop

pal([1,2,2]).

Not a palindrome

1true


6. Write a Prolog program to remove the Nth item from a list.

 6. Write a Prolog program to remove the Nth item from a list. 

del(1,[_|T],T).

del(P,[X|Y],[X|R]):-

 P1 is P-1,

 del(P1,Y,R).


OUTPUT:

del(2,[1,2],X).

X = [1]

Next101001,000 Stop

del(3,[1,2,3,4],X).

X = [1, 2, 4]

Next101001,000 Stop

del(5,[1,2,3,4],X).

false


5. Write a prolog program, insert_nth(item, n, into_list, result) that asserts that result is the list into_list with item inserted as the n‘th element into every list at all levels.

 5. Write a prolog program, insert_nth(item, n, into_list, result) that asserts that result is the list into_list with item inserted as the n‘th element into every list at all levels. 


insert_nth(Item,1,List,[Item|List]).

insert_nth(Item,Pos,[H|List],[H|Result]):-Pos1 is Pos-1,insert_nth(Item,Pos1,List,Result).





OUTPUT:

insert_nth(5,1,[2],X).
X = 2


insert_nth(5,1,[9,8,7,6],Y).
Y = [59876]

4. Write a prolog program to calculate the nth Fibonacci number.

 4. Write a prolog program to calculate the nth Fibonacci number. 


fib(0, 1) :- !.

fib(1, 1) :- !.

fib(N, F) :-        N > 1,        N1 is N-1,        N2 is N-2,        fib(N1, F1),        fib(N2, F2),        F is F1+F2.



OUTPUT:

fib(5N.
N = 8

3. Write a prolog program to calculate the factorial of a given number.

 3. Write a prolog program to calculate the factorial of a given number. 


fact(0,1).

fact(N,F):-(  N>0,  N1 is N-1, fact(N1,F1),  F is N*F1).



OUTPUT:

fact(5,N).

N = 120

Next101001,000 Stop


2. Write a prolog program to find the maximum of two numbers.

 2. Write a prolog program to find the maximum of two numbers.


max(X,Y):-(   X=Y ->write('both are equal') ;

 X>Y ->  (  Z is X,   write(Z), write('  is max')  )

  ;

  (  Z is Y,   write(Z),write(' is max')  ) ).


OUTPUT:

max(50,50).

both are equal

1true

max(50,5).

50is max

1true


1. Write a prolog program to calculate the sum of two numbers.

1. Write a prolog program to calculate the sum of two numbers. 

sum(X,Y):-S is X+Y,write(S).


OUTPUT: