Each exercise that follows requires that you use the given programs
either as they are, or in some modified form, to answer some questions.
It is important that you make notes of how you went about answering
the questions. It is through error and trial that you will learn most
in this exercise.
This program is designed to demonstrate a procedural aspect of PROLOG
- that is its ability to handle regular programming type things. You
will notice that the program has an internal goal, and that the clauses
section of the program contains one long task.
nl and write("please type in your name : ") and
readln(Name) and nl and
write(" Hello ",Name," and I really mean that").
You will notice that the WINDOW appears in the DIALOGUE box, and is
named (in the definition). Other things to notice : variables are
objects with labels beginning with a capital letter, nl means NewLine,
there are conventional readln and write statements (writeln doesn't
- change the makewindow definition to discover what the parameters
- change the prompts, add extra ones that prompt and respond
- choose new cursor positions
- Phone Book
This program is an extension of the greeting program. In it, we have
much the same prompting, linked to a collection of clauses that provide
a 'database'. It is PROLOGs aim to complete a goal if given one (if
possible) - after the user input, part of the goal searches the stored
clauses for instances that match. If there are any variables specified
in the goal, PROLOG tries to bind (attach, map, unify) the variable
with as many values as there are for it.
write("please type in a name : ") and
write("the PhNum is :",PhoneNum) and nl.
At the prompt, get prolog to display:
- the phone number of Fred
- all phone number-person combinations.
This program displays simple binding at the external goal. To find
similes for words, issue a goal with a variable as the second value
in the clause. Add some more clauses. See if you can add a section
of code that prompts the user for the word to search for, and displays
all similar words.
Hint: use an internal goal similar to the goal in /*phone book*/ but
you will need also to append a fail operator (the words and fail)
prior to the full-stop. This forces PROLOG to backtrack and search
for other solutions, and keep searching (with the aim of getting to
the full-stop and hence completing its goal) until it does not find
any more matches (and hence fail = true ---> terminate)
- Car Buyers Guide
This program utilises your skill at composing external goals
/*car buyers guide*/
brand, color = symbol
age = integer
mileage,price = real
Find answers to the following questions (writing down your goals as
- all green cars
- all red cars that are datsuns
- all cars whose mileage is less than 12000
- all cars that cost less than $4000
- all cars that have more than 100000 mileage yet cost more than
- all red cars that are more than 6 years old
- all cars that are less than 2 years old yet have more than 1000
- all cars that are volvos
- all cars that are blue that are not hondas
- The Luck Of The Draw
/*the luck of the draw*/
child = symbol
age = integer
Again, your skill at writing external goals is to be tested, using
variable binding to form expressions that result in output that:
- list all pairs of 9 year olds possible
- list all pairs of mixed ages possible
Now mutate the program (slightly) to include gender of the pupil (this
will mean altering the predicate and the clauses), then try to execute
- list all male 8 year old pairs possible
- list all mixed gender 9 year olds possible
- The Dynasty Database
/* The Dynasty Database */
/* Based on the Carrington Family from */
/* the TV series called Dynasty */
person = symbol
Download the Dynasty.txt file to your home
directory, rename it as dynasty.pro and then complete the following:
- From the data in the database draw a family tree of the Carrington
- Write external goals that answer yes or no to the following
- Blake is Adam's parent.
- Danny has Blake as a parent.
- Sammy Jo is a male
- Alexis is a parent of Falon
- Crystal is a female
- Blake is the male parent of Sammy-Jo.
- Danny is the male child of Blake.
- Crystal is the mother of Falon.
- Geoff is the father of Danny.
- Amanda is Christina's daughter.
- Write external goals that will extract the required information
in the following examples.
- Who are Falon's parents?
- What are the names of all the males?
- List the names of Blake's children.
- Who is Amanda's mother?
- Who are Blake's sons.
- Who is Danny's grandmother?
- EXTRA FOR EXPERTS: Add clauses and corresponding predicates
to the database so that the following relationships are defined
(hint: once a rule is defined it is allowed to be called by another
TEST your clauses by issuing goals that use them, and check they
deliver the correct responses.
HINT: the clause: is_mother_of(Mum,Child) :- is_parent_of(Mum,Child)
You will also need to define a predicate for each new type of
clause. (eg. is_mother_of(person,person)
- is_brother_of (full brother)