Revising for a prolog exam and working through past papers that we don't have answers for and I've got stuck halfway through a problem and I was really hoping for some help!
The question:
Write a predicate permute(Xs, Ys) that scrambles the elements of the list Xs to obtain the scrambled list Ys.
The query ?- permute([1,2,3], Ys). should succeed six times and give the following answers:
Ys = [1,2,3]
Ys = [2,1,3]
Ys = [2,3,1]
Ys = [1,3,2]
Ys = [3,1,2]
Ys = [3,2,1]
though not necessarily in that order.
We're advised to use a predicate we've already made, and so far I have this:
insert([], Y, [Y]).
insert([H|T], Y, [Y, H|T]).
insert([H|T], Y, [H|T2]) :- insert(T, Y, T2).
permute([], []).
permute([H|T], X) :- insert(T, H, X).
When I query it I only get half as many as I should, and I can't work out to switch the order of the list.
?- permute([1,2,3], Ys).
Ys = [1,2,3],
Ys = [2,1,3],
Ys = [2,3,1].
Any advice greatly appreciated!
insert/3andselect/3are completely equivalent to each other, just with re-arrangement of arguments. - lurkerselect/3inperm(List, [First|Perm]) :- select(First, List, Rest), perm(Rest, Perm).is the same as myinsert/3, I just can't work out what the recursive call would be. I've trued rearranging and matching the arguments and failed miserably - user9759943insert/3predicate on its own, independent ofpermute/2? If so, does it work correctly? My other comment was just that:insert(ListWithoutX, X, ListWithX)is the same asselect(X, ListWithX, ListWithoutX)(as defined in the Prolog library). If properly implemented, their behavior should be the same. - lurker