CSc 404 Assignment 5

Relate To Lists


Apr 3
45 pts
Apr 15

Create two (mostly unrelated) Prolog relations.

Create a prolog relation distin which takes an item and two lists. The third contains all the items from the second in the original order, with the first argument inserted between each, and at the front and the back. For instance:

?- consult(a5). true. ?- distin(x,[a,d,e],Y). Y = [x, a, x, d, x, e, x] ; false. ?- distin(s,[h,e,r,e],Z). Z = [s, h, s, e, s, r, s, e, s]. ?- distin(zz,[a],Y). Y = [zz, a, zz] ; false. ?- distin(zz,[],Y). Y = [zz] ; false. ?- distin(A,[t],[fred,t,fred]). A = fred. ?- distin(A,[t],[fred,t,bill]). false. ?- distin(E,[m,p,e],[r|Q]). E = r, Q = [m, r, p, r, e, r]. ?-

Create a Prolog relation lmerge which is true when the third argument is a list built from the first two (also lists) by alternately selecting items from each, in order, starting with the first. If one of the first two lists is longer, the extra elements appear at the end of the third, after the alternation. That is:

?- consult(a5). true. ?- lmerge([a,b,c],[x,y,z],X). X = [a, x, b, y, c, z] ; false. ?- lmerge([a,b,c,d,e],[x,y,z],X). X = [a, x, b, y, c, z, d, e] ; false. ?- lmerge([a,b,c],[x,y,z,p,l,s],X). X = [a, x, b, y, c, z, p, l, s]. ?- lmerge([a,b,c],[],Y). Y = [a, b, c] ; false. ?- lmerge([],[a,b,c],Y). Y = [a, b, c]. ?- lmerge([],[],Y). Y = [] ; false. ?- lmerge(A,B,[]). A = B, B = []. ?- lmerge(X,Y,[m,u,c,h]). X = [m, u, c, h], Y = [] ; X = [], Y = [m, u, c, h] ; X = [m, c, h], Y = [u] ; X = [m], Y = [u, c, h] ; X = [m, c], Y = [u, h]. ?-

Sometimes the system invites a request for more results, then doesn't find any. Your solution may do this for some of the same cases, or for different ones. (If you find a way to get rid of it, congratulations!) But it should report all the answers, once each, and no extras.


Put both relations in the same prolog file. When they work, are nicely formatted and documented, submit the file this form.