CSCI305.github.io

CSCI 305: Programming Languages

Prolog Part 1 Continued

Reading: Webster Ch. 19

Instructions

  1. Watch This Video - (19:04)
  2. Watch This Video - (16:09)
  3. Watch This Video - (19:57)
  4. Watch This Video - (15:37)
  5. Review the Lecture Slides
  6. Review the Example Code
  7. Attend Class and Complete the In Class Exercises
  8. Check Your Learning

In Class Exercises

Define your answers to the following exercises in terms of the base relations parent(X,Y), female(X), and male(X). To test your code, define some parent facts like those in the example code, along with appropriate female and male facts. But your solutions should be general enough to work with any set of such facts.

Exercise 1

Define a mother predicate so that mother(X,Y) says that X is the mother of Y.

Check Your Learning:

Solution:
mother(X, Y) :- female(X), parent(X, Y).

Exercise 2

Define a father predicate so that father(X,Y) says that X is the father of Y.

Check Your Learning:

Solution:
father(X, Y) :- male(X), parent(X, Y).

Exercise 3

Define a sister predicate so that sister(X,Y) says that X is the sister of Y. Be careful, a person cannot be her own sister.

Check Your Learning:

Solution:
sister(X, Y) :- parent(Z, X), parent(Z, Y), female(X), not(X = Y).

Exercise 4

Define a grandson predicate so that grandson(X, Y) says that X is the grandson of Y.

Check Your Learning:

Solution:
grandson(X, Y) :- parent(X, F), parent(F, Y), male(Y).