

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Homework 5: Prolog Due date: 3/14/2006, 11:59 PM.
The purpose of this assignment is for you to gain some experience designing and implementing Prolog programs. This assignment is broken into three parts. The first part is a fairly straight forward Prolog warmup. Part 2 involves writing a set of simple programs for manipulating lists and variables. Part 3 requires you to use Prolog’s control mechanism to implement a predicate for scheduling.
You will be given sets of facts of the following form:
An example database of facts is shown below:
course(ecs40, [ecs30], 4). course(ecs122a, [ecs100, ecs110], 3). .. . student(john, [ecs30, ecs40]). .. . instructor(jim, [ecs30, ecs110]).
Write the following separate queries:
a) Find all courses with 3 or 4 credits (fc course).
b) Find all courses whose immediate pre-requisite is ecs110 (prereq 110 ).
c) Find names of all students in ecs140a (ecs140a students).
d) Find the names of all instructors who teach john’s courses (instructor names).
e) Find the names of all students who are in jim’s class (students).
f) Find all pre-requisites of a course (allprereq). (This will involve finding not only the immediate pre- requisites of a course, but pre-requisite courses of pre-requisites and so on.)
When you are through testing your queries interactively, prepare them for use by the testing program by defining predicates with the names shown above.
The goal of this part of the homework is to familiarize you with the notions of lists and predicate definitions in Prolog. This part requires you to define a number of simple predicates: (a) Write a predicate, all length, that takes a list and counts the number of atoms that occur in the list at all levels.
?- all length([a, b, c], X) X = 3; no ?- all length([a, [b,c], [d,[e,f]]], Y) Y = 6; no
(b) Define a predicate, equal a b(L), which returns true if L contains an equal number of a and b terms. (c) Define a predicate, swap prefix suffix(K, L, S), such that swap prefix suffix(K, L, S) is true if
?- swap prefix suffix([c, d], [a, b, c, d, e], S). yes. S=[e, c, d, a, b] ?- swap prefix suffix([c, e], [a, b, c, d, e], S). no. ?- swap prefix suffix(K, [a, b, c, d, e], [b, c, d, e, a]). yes. K=[a]
Look at the class notes for the definitions of prefix, suffix, and sublist. (d) Define a predicate, palin(A) that is true if the list A is a palindrome, that is, it reads the same back- wards as forwards. For instance, [1, 2, 3, 2, 1] is a palindrome, but [1, 2] is not. (e) A good sequence consists either of the single number 0, or of the number 1 followed by two other good sequences: thus, [1,0,1,0,0] is a good sequence, but [1,1,0,0] is not. Define a relation good(A) that is true if A is a good sequence.
Write a logic program to solve the following puzzle: A farmer must ferry a wolf, a goat, and a cabbage across a river using a boat that is too small to take more than one of the three across at once. If he leaves the wolf and the goat together, the wolf will eat the goat, and if he leaves the goat with the cabbage, the goat will eat the cabbage. How can he get all three across the river safely? Hints: Define the following predicates: