Sunday, February 16, 2025

15. 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.

 

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.

% 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.

evenlength:-
 write('true --> even').
oddlength:-
 write('true --> odd').

oddeven([H|T]):-
 length(T,L),
 L>=0 ->
 (
  L1 is L+1,
  L2 is mod(L1,2),
  L2=:=0 ->
   evenlength
  ;
   oddlength
 ).
% Output
Prolog program to implement two predicates evenlength(List) and oddlength(List).
Prolog program to implement two predicates evenlength(List) and oddlength(List).

14. Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List using cut predicate.

 

Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List using cut predicate.

% Write a Prolog program to implement maxlist(List,Max) so that Max is the 
% greatest number in the list of numbers List using cut predicate.

max2([H],H).
max2([H|T],R):-
 max2(T,M1),
 H>=M1,
 R is H,!.
max2([H|T],R):-
 max2(T,M1),
 H<M1, 
 R is M1.
% Output
Prolog program to implement maxlist(List,Max)
Prolog program to implement maxlist(List,Max) using cut predicate.

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

 

Write a Prolog program to implement palindrome(List).

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

% append is the inbuilt function.

palind([]):- write('palindrome').
palind([_]):- write('palindrome').
palind(L) :-
 append([H|T], [H], L),
 palind(T)
 ;
 write('Not a palindrome').
% Output
Prolog program to implement palindrome(List).
Prolog program to implement palindrome(List).

12. Write a Prolog program to implement reverse(List,ReversedList) that reverses lists.

 

Write a Prolog program to implement reverse(List,ReversedList) that reverses lists.

% Write a Prolog program to implement reverse(List,ReversedList) that reverses
% lists.


/* Reverse of the list. */

reverse([H|T],R):-
 length(T,L),
 L>0 ->
 (
  reverse(T,R1),
  /* write(R1), */
  R is H
 )
 ;
 R is H.
% Output
Prolog program to implement reverse(List,ReversedList) that reverses lists.
Prolog Program to reverse the elements in the list.

11. Write a Prolog program to implement GCD of two numbers.

 

Write a Prolog program to implement GCD of two numbers.

% Write a Prolog program to implement GCD of two numbers.

/* GCD of two numbers. */
gcd(X,0,X).
gcd(X,Y,Z):- 
 R is mod(X,Y),
 gcd(Y,R,Z).
% Output
Write a Prolog program to implement GCD of two numbers.
Write a Prolog program to implement GCD of two numbers.

10 Write a Prolog program to implement sumlist(List,Sum) so that Sum is the sum of a given list of numbers List.

 

Write a Prolog program to implement sumlist(List,Sum) so that Sum is the sum of a given list of numbers List.

% Write a Prolog program to implement sumlist(List,Sum) so that Sum is the sum of
% a given list of numbers List.

/* Sum of the numbers from the list. */

sumlist([],0).
  
sumlist([H|T],R):-
  sumlist(T,R1),
  R is H+R1.
% Output
Prolog program to implement sumlist(List,Sum) so that Sum is the sum of a given list of numbers List.
Prolog Program to find the sum of numbers in the list.

9. Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List.

 

Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List.

% Write a Prolog program to implement maxlist(List,Max) so that Max is the 
% greatest number in the list of numbers List.


/* Max of n-#.s in a list. */
maxlist([H|T],R):-
 length(T,L),
 L>0 ->
 (
  maxlist(T,R1),
  (
   H > R1 -> 
     R is H
    ;
     R is R1
  )
 ) 
 ;
 R is H.
% Output
Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List.
Prolog Program to find maximum number in the list of N numbers.

8. Write a Prolog program to implement max(X,Y,Max) so that Max is the greater of two numbers X and Y.

 

Write a Prolog program to implement max(X,Y,Max) so that Max is the greater of two numbers X and Y.

% Write a Prolog program to implement max(X,Y,Max) so that Max is the greater of
% two numbers X and Y.

/* Max of two #.s */

/* without list. */
max(X,Y,R):-
 X>=Y -> 
  R is X, 
  write(R)
 ;
  R is Y,
  write(R).


/* with list. */
grandiose([H|T],R):-
 H>T -> 
  R is H,
  write(R)
  ;
  R is T,
  write(T).
% Output
 Prolog program to implement max(X,Y,Max) so that Max is the greater of two numbers X and Y.
 Prolog program to implement max(X,Y,Max) so that Max is the greater of two numbers X and Y.

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.

 

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.

% 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.

/*delete a number in the list. */
delte(1,[_|T],T).
delte(P,[X|Y],[X|R]):-
 P1 is P-1,
 delte(P1,Y,R).

/* delete before and after. */
daltob(P,L,R):-
 P1 is P-1,
 delte(P1,L,R1),
 /* delete before. */
 delte(P,R1,R).
 /* delete after. */

% Output
Prolog Program to delete before and after of the nth element in the list.
Prolog Program to delete before and after of the nth element in the list.

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

 

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

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

/*delete a number in the list. */
delte(1,[_|T],T).
delte(P,[X|Y],[X|R]):-
 P1 is P-1,
 delte(P1,Y,R).
% Output
Prolog program to remove the Nth item from a list
                        Prolog program to remove the Nth item from a list

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.

 

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.

% 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.

/*append(l1,l2,l3).*/
mem(X,[X|_]).
mem(X,[_|T]):- mem(X,T).
/* insert  a number in the list. */ 
 insert(L,[X|Y],[L|_]).
 insert(L,P,[X|Y],[X|M]):-
 P>1,
 P1 is P-1,
 insert(L,P1,Y,M).
 insert(L,1,[X|Y],M):- append([L],[X|Y],M).
% Output
Prolog Program to insert element in the list
Prolog Program to insert element in the list. 

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

 

Write a Prolog program to implement append for two lists.

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


/*concatenate*/
conc([],L,L).
conc([X|M],N,[X|Q]):-
 conc(M,N,Q). 

% Output
Prolog program to implement append for two lists
                      Prolog program to implement append for two lists

3. Write a Prolog program to calculate the sum of two numbers.

 


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

%  So, what is the function for above mentioned problem.
sum(X,Y):-
 S is X+Y,
 write(S).
% Output
Prolog program to calculate the sum of two numbers
                             Prolog program to calculate the sum of two numbers

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

 


% 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)
  )
  ;
  (
  Z is Y, 
  write(Z)
  ) 
).
% Output
Prolog Program to find maximum of two numbers.
Prolog Program to find maximum of two numbers.

15. 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.

  Write a Prolog program to implement two predicates evenlength(List) and oddlength(List) so that they are true if their argument is a list ...