CS计算机代考程序代写 prolog 11/2/21, 1:37 PM Homework 3

11/2/21, 1:37 PM Homework 3

https://canvas.vt.edu/courses/135925/assignments/1324732 1/3

Homework 3
11/10/2021

50 Possible Points

IN PROGRESS
Next Up: Submit Assignment

Unlimited Attempts Allowed

Attempt 1 Add Comment

Details

1. (5 points) Write a Prolog predicate makeset(L1, L2) that takes list L1 of integers and removes the
repeating elements. The result is returned in list L2. For example, makeset([1,3,4,1,3,9],Set).
would return [1,3,4,9]

2. (5 points) Recall the triple predicate presented in class:

triple(X, XXX) :- append(X, X, XX),
append(XX, X, XXX).

If X=[1,2,3], this will produce [1, 2, 3, 1, 2, 3, 1, 2, 3] in XXX. Rewrite this predicate so that for X=
[1, 2, 3], XXX is set equal to [1, 1, 1, 2, 2, 2, 3, 3, 3].

3. (10 points) Recall the bubblesort code presented in class:

bubblesort(X,Y) :- append(M, [A,B|N], X),
A > B,
append(M, [B,A|N], S),
bubblesort(S, Y).
bubblesort(L,L).

As demonstrated in class, after finding the right solution, the predicate goes on to find a lot of
spurious solutions. “Fix” this code to only give the right solution. You are welcome to use cut (!) for
this exercise.

4. (30 points) You are given the following PROLOG clauses:

female_author :- author(X),
write(X),
write(‘ is an author’),
nl,
female(X),
write(‘ and female’),
nl.

female_author :- write(‘no such luck!’),
nl.

author(X) :- name(X). Submit Assignment

11/2/21, 1:37 PM Homework 3

https://canvas.vt.edu/courses/135925/assignments/1324732 2/3

Choose a submission type

author(X) :- write(‘no more found!’),
nl,
fail.

name(sartre).
name(calvino).
name(joyce).

female(murdoch).
female(bembridge).

Strategically place cuts (!) in the code to achieve each of the following desired outputs (i.e., each
item below is a separate and independent part of the exercise). You are allowed to put only one
cut per desired output. There are therefore five parts to this exercise.

1. sartre is an author no more found! no such luck!

2. sartre is an author calvino is an author joyce is an author no more found!

3. sartre is an author no such luck!

4. sartre is an author

5. sartre is an author calvino is an author joyce is an author no such luck!

Add comments to your code to indicate where the cuts would go. For example,

author(X) :- name(X) /* 3 */. if this is where you want the cut to go for #3 above. You can
copy and paste the code and add comments in the one code for all the cuts.

Upload More

Canvas Files

Choose a file to upload
File permitted: PL

Submit Assignment

11/2/21, 1:37 PM Homework 3

https://canvas.vt.edu/courses/135925/assignments/1324732 3/3

Submit Assignment