Prolog program to traverse a list

Traverse a list: domains namelist = name* name = symbol predicates traverse(namelist) clauses traverse([]). traverse([Head|Tail]):- write(Head),nl, traverse(Tail). Also try with: domains namelist=symbol* predicates member(symbol,namelist) club_member(symbol) clauses member(X,[X|_]). member(X,[_|Tail]):- member(X,Tail). club_member(Name):- member(Name,[a,b,c,d,e]). Continue reading Prolog program to traverse a list

Prolog program to find minimum and maximum

Find out the MAXIMUM and MINIMUM values among three numbers: domains predicates max(integer,integer,integer) min(integer,integer,integer) maxmin(integer,integer,integer) clauses max(A,B,C):- A>B, A>C, write(A). max(A,B,C):- A>B, write(C). max(_,B,C):- B>C, write(B). max(_,_,C):- write(C). min(A,B,C):- A<B, A<C, write(A). min(A,B,C):- A<B, write(C). min(_,B,C):- B<C, write(B). min(_,_,C):- write(C). maxmin(A,B,C):- max(A,B,C), min(A,B,C). Continue reading Prolog program to find minimum and maximum

Simple Programs for Prolog

Maximum value from the list: domains numlist=integer* predicates max(numlist,integer) maxim(numlist) clauses maxim(L):- max(L,0). max([],Maxim):- write(“\nmaximum=”),write(Maxim). max([H|T],Maxim):- Maxim=H,max(T,Maxim). Minimum value from the list: domains numlist=integer* predicates max(numlist,integer) maxim(numlist) clauses maxim(L):- max(L,32767). max([],Maxim):- write(“\nmaximum=”),write(Maxim). max([H|T],Maxim):- MaximH,max(T,H). Find average value from the list: domains int = integer* predicates avg(int, integer, integer) clauses avg([],A,T):- Z = A/T, write(Z). avg([Head|Tail], A, T):- Z = A + Head, TT = T … Continue reading Simple Programs for Prolog

Prolog program to find GCD and LCM

To find the GCD among two numbers: predicates go gcd(integer,integer) clauses go:- write(“Enter the first input X1::”), readint(X1), write(“Enter the second input X2::”), readint(X2),gcd(X1,X2). gcd(X1,0):- write(X1),nl. gcd(0,X2):- write(X2),nl. gcd(X1,X2):- X1=X2,write(X1),nl. gcd(X1,X2):- X1>X2, X3=X1 mod X2,gcd(X2,X3). gcd(X1,X2):- X1<X2, gcd(X2,X1). Find the LCM of given numbers: predicates go gcd(integer,integer,integer) clauses go:- write(“enter no1=”), readint(X), write(“enter no2=”), readint(Y), gcd(X,Y,Z), P=(X*Y)/Z, write(P). gcd(A,0,Z):- Z=A. gcd(0,B,Z):- Z=B. gcd(A,B,Z):- A>B, C=A … Continue reading Prolog program to find GCD and LCM