Professional Documents
Culture Documents
COS4851 Assignment 1 2024
COS4851 Assignment 1 2024
2024
Assignment 1
School of Computing
BARCODE
Open Rubric
COS4851/0/2024
TOTAL MARKS: 60
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]
Use one predicate to represent all the known details regarding a particular dog and one
predicate for a particular dog breed, eg
dog(_,_,_).
breed(_,_,_).
(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]
?- exp(2,3,R).
R = 8 ;
no
Question 3 [10]
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.
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?
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):
©
Unisa 2024