Question

In: Computer Science

Given the set of facts: eats(pete, burger). eats(susan, pizza). eats(martha, pizza). eats(john, burger). eats(pete, pizza). eats(martha,...

Given the set of facts:

eats(pete, burger).

eats(susan, pizza).

eats(martha, pizza).

eats(john, burger).

eats(pete, pizza).

eats(martha, oranges).

eats(susan, oranges).

Translate each of the questions below to a Prolog query and show the result of its execution (if applicable, show multiple answers):

(a) Who eats burger?

(b) Who eats pizza and oranges?

(c) What does Pete eat?

Solutions

Expert Solution

The answer is given below:

For facts:

eats(pete, burger).

eats(susan, pizza).

eats(martha, pizza).

eats(john, burger).

eats(pete, pizza).

eats(martha, oranges).

eats(susan, oranges).

Prolog query for each questions:

a) for Who eats burger? prolog Query is given below:

eats(X,burger)

above query will return multiple answers pete and john because pete and john both eats burger.

In query X is a variable, prolog will try to match expression eats(X,burger) with facts from top to bottom and return answer.

Output:

X = pete

X = john

b) for Who eats pizza and oranges? prolog Query is given below:

eats(Y,pizza),eats(Y,oranges)

To find who eats pizza and oranges comma work as and. first it will find that pete eats pizza it will fails, so it goes to second fact and check susan eats pizza ,then check second part susan eats oranges it satisfy so it gives answer as Y=susan simillarly it check all facts.

Output:

Y = susan

Y = martha

c) for What does Pete eat? prolog Query is given below:

eats(pete,Y)

In query Y is a variable, prolog will try to match expression eats(pete,Y) with facts from top to bottom and return answer.

Output:

Y = burger

Y = pizza


Related Solutions

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT