Gain Infiniti: Prolog
Showing posts with label Prolog. Show all posts
Showing posts with label Prolog. Show all posts

Fibonacci Series Program in Prolog

 fib(0,1).
fib(1,2).
fib(X,Y):- X1 is X-1,fib(X1,Y1),X2 is X-2,fib(X2,Y2),Y is Y1+Y2,!.


 Output
      fib(6,P).
P=8

Factorial program in Prolog

fac(0,1):-!.
fac(1,1):-!.
fac(X,Y):-X1 is X-1,fac(X1,Y1),Y is Y1*X,!.


 Output
fac(5,P).
P=120

Multiply Program in Prolog

mul(_,0,0):-!.
mul(X,Y,N):-Y1 is Y-1,mul(X,Y1,N1),N is N1+X,!.

 
 Output
 mul(3,4,P).
P=12
      

Delete Particular Element From a Given List in Prolog


del(X,[X|Tail],Tail).
del(X,[Y|Tail],[Y|Tail1]):-del(X,Tail,Tail1).


  OUTPUT:-
 del(a,[a,b,c,d],P).
 P=["b","c","d"]

FINDING LAST ELEMENT OF LIST /2 ND/LAST/N TH ELEMENT IN PROLOG


find(1,[X|_],X):-!.
find(N,[_|T],Y):-N1 is N-1,find(N1,T,Y).

OUTPUT:-
find(3,[a,b,c,d],P).
 P=c


Reversing a List in Prolog


concatenation([],L,L).
concatenation([X|L1],L2,[X|L3]):-concatenation(L1,L2,L3).

rev([],[]).
rev([H|T],X):-rev(T,Y),concatenation(Y,[H],X).

OUTPUT:-

Go rev([b,d,x,a],P).
P=[“a”,”x”,”d”,”b”]

Merging Two Ordered List in Prolog


merge(X,[],X).
merge([],Y,Y).
merge([X|T],[Y|T1],[X|L]):-X<=Y,merge(T,[Y|T1],L).
merge([X|T],[Y|T1],[Y|L]):-X>Y,merge([X|T],T1,L).

OUTPUT:-

 merge([a,b],[b,c],L)
L=["a","b","b","c"]

PROGRAM FOR IMPLEMENTING MEDICAL DIAGNOSTIC SYSTEM IN PROLOG


symptom(charlie,fever).
symptom(charlie,headache).
symptom(charlie,runnynose).
symptom(charlie,rash).
hypothesis(patient,measles) :- symptom(Patient,fever),symptom(Patient,cough), symptom(Patient,conjunctive), symptom(Patient,runnynose), symptom(Patient,rash).
hypothesis(Patient,germanmeasles):- symptom(Patient,fever),symptom(Patient,headache),symptom(Patient,runnynose),symptom(Patient,rash).
hypothesis(Patient,flu):- symptom(Patient,fever),symptom(Patient,headache),symptom(Patient,bodyache),
symptom(Patient,chills), symptom(Patient,sorethrought),symptom(Patient,cough), symptom(Patient,conjunctive), symptom(Patient,conjunctive),symptom(Patient,runnynose).
hypothesis(Patient,commoncold):-symptom(Patient,headache),symptom(Patient,runnynose), symptom(Patient,snuzing),symptom(Patient,chills),symptom(Patient,sorethrought).
hypothesis(Patient,mumps):-symptom(Patient,fever),symptom(Patient,swallenglands).
hypothesis(Patient,chikenpox):-symptom(Patient,fever),symptom(Patient,rash),symptom(Patient,bodyache).
hypothesis(Patient,whooping-cough):-symptom(Patient,runnynose),symptom(Patient,snuzing),symptom(Patient,cough).

OUTPUT

 hypothesis(Charlie,P).
P=germanmeasles

Palindrome in Prolog


concate([],L,L).
concate([X|L1],L2,[X|L3]):-concate(L1,L2,L3).
reverse([],[]).
reverse([H|T],X):-reverse(T,Y),concate(Y,[H],X).
pal(X):-reverse(X,X).

OUTPUT

 pal([b,a,c,a,b]).
Yes

PERMUTATION IN PROLOG


delete(X,[X|Tail],Tail).
delete(X,[Y|Tail],[Y|Tail1]):-delete(X,Tail,Tail1).
insert(X,List,B):-delete(X,B,List).
per([],[]).
per([X|L],P):-per(L,L1),insert(X,L1,P).

 Output
 per([a,b,c],P).
 P=["a","b","c"]
 P=["b","a","c"]
 P=["b","c","a"]
 P=["a","c","b"]
 P=["c","a","b"]
 P=["c","b","a"]

Program For Concatenation OR Append in Prolog


con([],L,L).
con([X|L1],L2,[X|L3]):-con(L1,L2,L3).

 Output
con([a,d,b],[c,g],P).
P=[“a”,”d”,”b”,”c”,”g”]

DELETE ALL OCCURRENCE OF AN ELEMENT IN PROLOG


delete([],A,[]).
delete([H|T],A,Result) :- H=A, delete(T,A,Result).
delete([H|T],A,[H|Result]) :- delete(T,A,Result).

Output
delete([a,b,c,a],a,X)
X=["b","c"]
X=["b","c","a"]
X=["a","b","c"]
X=["a","b","c","a"]
4 Solutions

POWER OF A NUMBER IN PROLOG


power(1,N,N).
power(X,N,P):- X1 is X-1,power(X1,N,P1),P is N*P1.

 Output 
 power(3,3,P)
P=27

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 

Design By Manish and Ranjan