Anda di halaman 1dari 23

# Ques.1.

/* Facts--- */

ako(chair, furniture). ako(chair, seat). isa(your_chair, chair). isa(you, person). made_of(your_chair, wood). colour(wood, brown). belongs_to(your_chair, you).

/* Queries--

## 4. Determine whether your chair belongs to a person.

Solutions---

output--

X = wood Y = brown

yes

2. Determine whether your chair is a seat. 3. Determine whether your chair is a furniture.

## Ans-- isa(your_chair, X),ako(X,Y).

output--

X = chair Y = furniture ? ;

X = chair Y = seat

yes

## 4. Determine whether your chair belongs to a person.

Ans-- belongs_to(X,you),isa(you,Y).

output--

X = your_chair Y = person

yes

*/

## Facts and Rules:-

start:- write('input a= '),read(A), write('input b= '),read(B), write('input c= '),read(C), A >= 0,B>= 0,C >= 0, /* must be positive */ A < B+C,B< C+A,C < A+B, write('These numbers are the sides of a triangle.').

Queries :-

1. | ?- start. input a= 3. input b= 4. input c= 5. These numbers are the edges of a triangle.

## 2. | ?- start. input a= 18.

input b= 34. input c= 23. These numbers are the edges of a triangle.

yes

3. | ?- start. input a= 12. input b= 13. input c= 14. These numbers are the edges of a triangle.

yes

## Ques3.-Problem of likes and not likes.

Facts:-

likes(john, susie). likes(X, susie). likes(john, Y). likes(john, Y), likes(Y, john).

## likes(john, susie); likes(john,mary). /* John likes Susie or John likes Mary */

not(likes(john,pizza)).

## /* John does not like pizza */

Rules :likes(john,susie) :- likes(john,mary). /* John likes Susie if John likes Mary*/ friends(X,Y) :- likes(X,Y),likes(Y,X). hates(X,Y) :- not(likes(X,Y)). /* X and Y are friends if they like each other */ /* X hates Y if X does not like Y*/

enemies(X,Y) :- not(likes(X,Y)),not(likes(Y,X)). /* X and Y are enemies if they don't like each other */

Queries(with output):-

1. | ?- hates(john,Y).

Y = pizza

yes

2.?- friends(john,susie).

no

3. | ?- enemies(john,marry).

no

4. | ?- friends(john,marry).

no

5. | ?- likes(X,Y).

X = john Y = susie ?

## (15 ms) yes

Ques4.

/* Facts---- */

animal(mammal, tiger, carnivore, stripes). animal(mammal, lion, carnivore, mane). animal(mammal, hyena, carnivore, ugly). animal(mammal, zebra, herbivore, stripes).

/*

Queries---

Solutions---

## 1. Add a rule that writes a list of all the animal names.

Ans.-- animal_name(X).

output--

X = tiger ? ;

X = lion ? ;

X = hyena ? ;

X = zebra ? ;

X = eagle ? ;

X = sparrow ? ;

X = snake ? ;

X = lizard

## 2. list all animal name which is small.

Ans.-- animal_name(X,small).

output--

X = sparrow ? ;

X = lizard ? ;

no

## 3. list all animal name which is mammal.

Ans.-- animal_name(X,mammal).

output--

X = tiger ? ;

X = lion ? ;

X = hyena ? ;

X = zebra ? ;

(16 ms) no

## 4. list all animal which is bird.

Ans.-- animal_name(X,bird).

output--

X = eagle ? ;

X = sparrow ? ;

no

## 5. List all animal which is carnivore.

Ans.-- animal_name(X,carnivore).

X = tiger ? ;

output--

X = lion ? ;

X = hyena ? ;

X = eagle ? ;

X = snake ? ;

(16 ms) no

## 6. find the name of animal which is carnivore as well as ugly.

Ans.-- animal(X,Y,carnivore,ugly).

output--

X = mammal Y = hyena ? ;

no

*/

## /*Facts and Rules*/

start:-sum,n1. sum:- write('Enter value of X= '),read(X), write('Enter value of Y= '),read(Y), S is X+Y, write('Sum is '),write(S).

/*queries*/

/*| ?- start.

| ?- start.

| ?- start.

Sum is 5.7 */

## power(N,0,1):- !. power(N,K,R):- K1 is K-1,power(N,K1,R1),R is R1*N.

Query :1.

| ?- power(2,3,R).

R=8

yes 2.

| ?- power(3,3,R).

R = 27

yes

3. | ?- power(4,3,R).

R = 64

Yes

## fact(0,1). fact(N,R):- fact(N1,R1),N is N1+1,R is R1*N.

Query:1. | ?- fact(4,R).

R = 24 ?

yes

2. | ?- fact(5,R).

R = 120 ?

yes

3. | ?- fact(3,R).

R=6?

yes | ?-

Ques8.-Family problem.

Facts:-

Rules:-

## sibling_of(X,Y) :- !,father_of(Z,X),father_of(Z,Y),X\=Y. sibling_of(X,Y) :- !,mother_of(Z,X),mother_of(Z,Y),X\=Y.

Queries :-

1. father_of(X,paul).

X = joe ?

yes

2. | ?sibling_of(X,paul).

X = mary ?

yes

3. | ?- daughter_of(X,Y).

X = mary Y = joe ?

Yes

## Ques9.Program to to check planet and satellite with the following facts.

Facts :-

star(sun). orbit(earth, sun). orbit(moon, earth). orbit(saturn, sun). orbit(jupiter, sun). orbit(ioa, jupiter). orbit(casini, saturn). orbit(titan, saturn).

Rules :-

Queries:-

1.

## | ?- orbit(X,Y). X = earth Y = sun ? yes 2. | ?- planet(earth).

yes

3. | ?- satellite(casini). Yes

Facts &Rules :-

## size([],0). size([H|T],N) :- size(T,N1), N is N1+1. % or size([_|T],N) :- size(T,N1), N is N1+1.

Queries :-

1.

| ?- size([1,2,3,4],N).

N=4

yes 2.

| ?- size([bill,ted,ming,pascal,nat,ron],N).

N=6

yes

3.

N=5

yes