Balogun Prolog2
Balogun Prolog2
Balogun Prolog2
This assignment gives you practice with Prolog by writing rules for new predicates.
● 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.).
?- del3([], X).
false
?- del3([1], X).
false
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).
?- 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.
?- 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.