Sunteți pe pagina 1din 4

1) has(jack,apples). has(ann,plums). has(dan,money). fruit(apples). fruit(plums). /* lists the clauses */ ?- listing(fruit). fruit(apples). fruit(plums). ?- listing(has). has(jack,apples). has(ann,plums). has(dan,money).

/* what has Jack? */ ?- has(jack,X). X = apples /* who has apples and who has plums? */ ?- has(X,apples),has(Y,plums). X = jack Y = ann

2) parent(abraham,ismael). parent(abraham,isaac). parent(isaac,esau). parent(isaac,iacob). /* The children of Abraham */ ?- parent(abraham,X). X = ismael Y = isaac Yes /* Did Abraham have children? */ ?- parent(abraham,_). Yes /* The father of Esau? */ ?- parent(Father,esau). Father = Isaac

3) arc(a,b). arc(a,c). arc(b,e). arc(b,f).

arc(b,c). arc(a,d). arc(e,f). arc(f,g). path(X,Y):- arc(X,Y). path(X,Y):- arc(X,Z),path(Z,Y).

/* is there any path from a to b? */ ?- path(a,b). yes /* is there any path from c to g? */ ?- path(c,g). No

4) WAP for Add two numbera. start:- sum,nl. sum:write('X= '),read(X), write('Y= '),read(Y), S is X+Y, write('Sum is '),write(S).

5) likes(mary,food). likes(mary,wine). likes(john,wine). likes(john,mary). /* John likes Food? */ | ?- likes(john,food). no. /* John likes anyone who likes wine */ | ?- likes(john,X). X=wine ? ; X=mary Yes 6) WAP for find maximum number between two numbers max(X,Y,X):- X >= Y. max(X,Y,Y):- Y > X. ?- max(5,3,R). R=5 No

7) Calculate arithmetic expressions. ?- X is 5+4, Y is 5-4. X=9 Y=1 ?- X is 5*4, Y is 2^3. X = 20 Y=8 ?- X is 234556^100. Error 0:Arithmetic Overflow ?- X is 5/3, Y=5//3. X=1.66666667 Y=1 ?- X is sqrt(3),Y is 3^0.5. X = 1.73205080756888 Y = 1.73205080756888 ?- X is 8 mod 3. X=2 ?- Y is 10^3 * (1+1) + 3. Y = 2003 ?- halt. Exit from Prolog 8) WAP for find absolute value for given number. abs(X,X):- X >= 0, !. abs(X,Y):- Y is -X.

?- abs(0,R). R=0 ?- abs(-9,R). R=9 ?- abs(-9,9). Yes ?- abs(-9,8). No ?- abs(I,8). I=8

9) WAP for find factorial of given number. fact(0,1). fact(N,R):- fact(N1,R1),N is N1+1,R is R1*N. Factorial of n is n!= 1 * 2* 3 * 4 *... * n

?- fact(4,R). R = 24 10) WAP for find circle of area and circumference from given radius. area:- write('Radius '),read(R), write('Area is '),A is 3.14*R*R,write(A),nl, write('Circumference is '),C is 2*3.14*R,write(C),nl. | ?- area. Radius 2. Area is 12.56 Circumference is 12.56

S-ar putea să vă placă și