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.

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