Q1. Write a program to find whether a given list is odd or even.
Program:
DOMAINS
int=integer
list=int*
PREDICATES
length(list,integer)
display(list)
CLAUSES
length([],0).
length([_|T],N):-length(T,N1),N=N1+1.
display(X):-length(X,N),(N mod 2)=0,write("EVENLENGTH"),nl.
display(X):-length(X,N),(N mod 2)=1,write("ODDLENGTH"),nl.
GOAL
display([9,7,5,3]).
Output:
Q2. Write a program to reverse a list.
Program:
domains
int=integer
list_element=int*
predicates
reverse(list_element,list_element)
rever(list_element,list_element,list_element)
clauses
reverse(L1,L2) :-rever(L1,L2,[]).
rever([],L2,L2).
rever([H|T],L2,T1) :- rever(T,L2,[H|T1]).
goal
reverse([4,5,6,7,8],X).
Output:
Q3. Write a program to translate a list of members between 0 to 9 to a list of corresponding words.
Program:
DOMAINS
int=integer
list=int*
PREDICATES
length(list)
display(int)
CLAUSES
length([]).
length([H|T]):-display(H),length(T).
display(H):-H=0,write("ZERO"),nl.
display(H):-H=1,write("ONE"),nl.
display(H):-H=2,write("TWO"),nl.
display(H):-H=3,write("THREE"),nl.
display(H):-H=4,write("FOUR"),nl.
display(H):-H=5,write("FIVE"),nl.
display(H):-H=6,write("SIX"),nl.
display(H):-H=7,write("SEVEN"),nl.
display(H):-H=8,write("EIGHT"),nl.
display(H):-H=9,write("NINE"),nl.
GOAL
length([3,5,6,8,7]).
Output:
No comments:
Post a Comment