You are on page 1of 5

COS4851/0/2024

Logic Based Reasoning


COS4851

2024

Assignment 1

School of Computing

This tutorial letter contains important information


about your module.

BARCODE

Open Rubric
COS4851/0/2024

DUE DATE: 09 July 2024

SUBMISSION PROCEDURE Written

TOTAL MARKS: 60

UNIQUE NUMBER: 813037

STUDY MATERIAL: Bratko, Chapters 1 to 3

Important: The main purpose of this assignment is to develop your skills regarding the use of
recursion and list manipulation, both of which are very important aspects of declarative
programming. You will also be required to define facts represented in a database and to construct
queries for retrieving information from a given database.

Please note that you need to submit screen shots of results for questions where a
program/procedure or query is required. This will assist us to see whether you obtained the correct
results and, if not, try to point out where you went wrong.

Your programs should also be robust. This means that it should check whether all the input
arguments for a specific procedure/predicate are legal. For example, if you know you are working
with integers, an input of any item that is not an integer is not acceptable.

Important note: It is not advisable that you search for Prolog solutions to the questions in this
assignment on the internet. You may find a solution to a specific problem but that will not assist
you in acquiring the necessary skills for mastering this programming paradigm.

2
COS4851/0/2024

Question 1 [17]

(a) Convert the following information into a Prolog program:


Nadia owns a Beagle named Sasha.
Sjanie owns a Labrador named Zappa.
Peter owns a Dalmation named Domino.
John owns a Boxer named Daisy.
Marni owns a Ridgeback named Ben.
Beagles are medium dogs and are used for hunting.
Bassets are also used for hunting.
Labradors are large dogs and are used as guide dogs.
German Shepherds are used as guard dogs.

Use one predicate to represent all the known details regarding a particular dog and one
predicate for a particular dog breed, eg
dog(_,_,_).
breed(_,_,_).

(b) Write Prolog queries to ask the following questions:


Does Peter own a guide dog?
Is there anyone who owns a German Shepherd?
Who is Domino’s owner?

(c) Give an execution trace similar to the trace in Figure 2.10 on page 48 of Bratko (p. 43 in
Bratko, 3rd edition) for the query:
Is there anyone who owns a German Shepherd?
that you defined in (b) above.

3
COS4851/0/2024

Question 2 [6]

Write a procedure exp(Base,Exponent,Result) such that if b and e are non-negative


integers, the goal exp(b,e,R) returns the result R = be.
For example:

?- exp(2,3,R).
R = 8 ;
no

Your definition of exp should be recursive. Implement the following algorithm:


- if the exponent is 0, the result is 1; else
- if the base is 0, the result is 0; else
- obtain the result by multiplying the base b by (the result obtained from the same base and
the predecessor of the exponent e), i.e. b * b e-1.

Question 3 [10]

Suppose each student at a certain school is associated with a record of information.


For example:
Name: Quinton Duminy
Gender: male
Grade: 12
Sport: soccer, hockey, cricket

a) Create a Prolog database containing data regarding the students in the school. (Add a few of
your own.) A student can participate in 0,1 or more sports.
b) Write Prolog clauses that will enable you to retrieve the following information using a
suitable query: Given a first name and surname, find the gender, grade, or different sports in
which the student participates (one clause for each of the these).
c) Find all the students that participate in a particular sport, such as soccer.

Question 4 [6]

Write a predicate average with two arguments: the first argument is a list of integers or reals,
and the predicate must match the second argument with the average of the numbers in the list.

For example, the query


?- average([1,2,3,4,5,6],X).
produces
X = 3.500000

Hint: Write a second predicate that recursively determines the sum of the elements in a list.

4
COS4851/0/2024

Question 5 [6]

Which of the following pairs of lists match (i.e. are unifiable)? If they don’t match, why not?
If they do match, what are the resulting bindings for the variables?

a) [[a, b, c, d, e]] and [X | Y]

b) [a, b, c, d, e] and [[ X ]|[ b, c, d, e ]]

c) [a, pred_b(1, mill_14), b, pred_c(cdc,[8, 9]), d, e] and


[a, X, b, pred_c(Y,Z), d, e]

Question 6 [15]

Let L1, L2 and L denote lists of terms. Write Prolog programs to realise the following:
(Note that the lengths of lists L1 and L2 should be equal in all cases):

a) Interleave alternate elements of L1 and L2 into L.


For example, if L1 = [a,b,c] and L2 = [1,2,3],
then L = [a,1,b,2,c,3].

Are the input-output roles of variables in your program reversible?

b) Transpose L1 and L2 into L. That is, if L1 = [a,b,c] and L2 = [1,2,3], then


L = [(a,1),(b,2),(c,3)].

Are the input-output roles of your variables reversible?

c) Suppose that L1 and L2 are lists of numeric values. Write a procedure


inner_prod(L1,L2,X) that defines X to be the inner-product of the two vectors L1
and L2.

The inner-product X is calculated as follows:


If we have two vectors [a1, a2,..., an] and [b1, b2,..., bn] then
X = a1×b1 + a2×b2 +... + an×bn.
Are the input-output roles of the variable in your program reversible?

©
Unisa 2024

You might also like