Uploaded by Bridgette Mtambo

COS4851-2022-Assignment 1

advertisement
ASSIGNMENT 01
Due date:
19 May 2022
Material to be tested: Weight
Chapters 1-3
towards year mark:
30%
Important: To test whether or not you have mastered the study material, you need to run your
programs using the Prolog interpreter provided. It is not sufficient to write the Prolog code without
testing it. You may not get the answers you expected. Please copy and paste the program code plus
some test results into the document you submit for this assignment. This way we will be able to see
whether you got the correct results. If not, we can assist you in finding why the program goes wrong.
Question 1
[14]
(a)
Convert the following information into a Prolog program:
Monika owns a Basset named Lara.
Waldo owns a Beagle named Oscar.
Sjanie owns a Labrador named Zappa.
Peter owns a Dalmation named Uzo.
John owns a Boxer named Daisy.
André owns a Labrador named Olga.
Beagles are medium dogs and are used for hunting.
Bassets are also used for hunting.
Labradors are used as guide dogs.
German Shepherds are used as watch dogs.
(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 Daisy’s owner?
(c)
Illustrate the procedural meaning of a Prolog program by using a trace similar to the trace in
Figure 2.10 on page 43 of Bratko. Use the query ‘does Peter own a guide dog?’ that you
defined in (b) above.
Question 2
[8]
Which of the following pairs of expressions are unifiable? If they are, give the resulting bindings for
the variables. Otherwise give reasons why they cannot be unified.
a) [[a, b, c]]
and
[X|Y]
b)
[A|A]
and
[ X , [ c, d, e ] ]
c) [ A , A ]
and
[ X | [ c, d, e ] ]
d) f(Z) + 1 – Y * 2
and
U – (1 + 2) * Z
Question 3
[6]
Write a procedure that has a list L1 as input and produces a list L of ordered pairs such that the first
element of the ordered pair is the position of the pair in the list and the second element of the ordered
pair is the element from L1 in the corresponding position. For example, if L1 = [ a, b, c, d ], the output
list should be:
L = [(1, a), (2, b), (3, c), (4, d)].
Question 4
[12]
Let L denote a list. Write Prolog procedures to do the following:
(a)
count(X,L,N): Count the number of occurrences of X in L to give the result N.
(b)
delete_all(X,L,L1): Delete all occurrences of X from L to give the result L1.
(c)
replace_all(X,L,Y,L1): Replace all occurrences of X in L with Y to give the result L1.
Question 5
[10]
Write a procedure that finds both the minimum and maximum values of elements in a list. If the list is
empty, the minimum and maximum should both be set to zero. If the list contains more than n
elements, an error message ‘list is too long’ should be displayed. (See Bratko, page 135 for a
discussion of the write predicate that can be used to display messages.) When the program traverses
the list to find the minimum and maximum, all negative numbers should be ignored.
Related documents
Download