Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Balogun Prolog2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Overview

This assignment gives you practice with Prolog by writing rules for new predicates.

Part I: Prolog Exercises (15 Points)

You should use use SWISH to complete these problems in Prolog:

● Open and use the SWISH to complete the exercises below.

● Click the blue Program button to open a new Prolog program.

● Type your predicates for this assignment in this new program.

● Enter your queries in the text box in the lower right corner.

Once you have completed the above steps and you are set up in SWISH, complete the
following problems on Prolog.

1. (5 points) Define a predicate del3 so that del3(X,Y) says that the list Y is the same
as the list X but with the third element deleted. (The predicate should fail if X has fewer
than three elements.).

Demonstrate that your program works by posing the following queries:

?- del3([], X).
false

?- del3([1], X).
false

?- del3([1, 2], X).


false

?- del3([1, 2, 3], X).


X = [1, 2]

?- del3([1, 2, 1, 3], X).


X = [1, 2, 3]
⚠️ Once you have a working solution, take screenshot(s) that include BOTH the code on

the left side and the executed result on the right side of the screen (see example
screenshot here) for each query.

2. (5 points) Design the predicate halve/3 that takes a list as its first argument and
returns two lists each with half the elements of the original list (similar to the function
halve we studied in ML).

Demonstrate that your program works by posing the following queries:

?- halve([], X, Y).
X = Y, Y = []

?- halve([1], X, Y).
X = [1],
Y = []
false

?- halve([1,2], X, Y).
X = [1],
Y = [2]

?- halve([1,2,3], X, Y).
X = [1, 3],
Y = [2]
false

?- halve([1,2,3,4], X, Y).
X = [1, 3],
Y = [2, 4]

⚠️ Once you have a working solution, take screenshot(s) that include BOTH the code on
the left side and the executed result on the right side of the screen (see example
screenshot here) for each query.

3. (5 points) Define a predicate myflip/2 whose arguments are both lists, the second
being the reverse of the first. For example, myflip([a,b,c],X) should give
X=[c,b,a]. You are not allowed to use Prolog built-ins like reverse/2.

Hint: You will find it useful to use the append/3 predicate covered in class. The
append/3 predicate is a built-in predicate in Prolog, so no need to type in the rules. Do
not use the built-in predicate reverse to implement the myflip predicate.

Demonstrate that your program works by posing the following queries:

?- myflip([a,b,c,d,e,f], X).
X = [f, e, d, c, b, a]

?- myflip([a1,a2,a3,a4], X).
X = [a4, a3, a2, a1]

⚠️ Once you have a working solution, take screenshot(s) that include BOTH the code on
the left side and the executed result on the right side of the screen (see example
screenshot here) for each query.

Submission
Submit your work for this assignment as a PDF file called lastname_prolog2.pdf, where
lastname should be replaced with your last name. Navigate to this assignment submission and
upload your file by clicking the gray Add a File button and locating these files on your computer.
Once you have successfully uploaded this file, click the blue Submit button to successfully
submit your work for this assignment.

You might also like