1

I'm a student learning prolog. I've written a function to remove duplicate elements from a list in two ways. One that retains the order of the elements and one that does not. The problem is my output for the one that retains order, it contains a reference to an unbound variable?

retain order:

rmDup([], R) :- !.
rmDup([A | L], R) :-
    member(A, R), !,
    rmDup(L, R). 
rmDup([A | L], [A | L2]) :-
    rmDup(L, L2).

?- rmDup([a,b,c,c,a,d,a], W).
W = [a,b,c,d|_A] ? ;

not retain order:

rmDup([],[]).
rmDup([A|S1],S2) :- member(A, S1), rmDup(S1, S2),!.
rmDup([A|S1],[A|S2]) :- rmDup(S1, S2).


rmDup([a,b,c,c,a,d,a], W).
W = [b,c,d,a] ? ;

Now the only significant difference that I can see between the two functions is the parameters for member. I think that the case where R is empty is good to consider, but I've had problems trying to write it.

rmDup([A | L], []) :- add A to empty List

is what I want to write, but I don't know how.

So, to summarize, how do I get rid of the damned |_A in the output for the first function?

false
  • 10,264
  • 13
  • 101
  • 209
slmyers
  • 767
  • 1
  • 8
  • 21
  • Your rule: `rmDup([], R) :- !.` doesn't instantiate `R`. What should `R` be in this case? (HINT: what does the rule mean?) – lurker Apr 16 '15 at 10:20

0 Answers0