Question about difference lists



Hi.

I am teaching myself prolog. As an exercise, I wrote a predicate
"unique" to find all the unique elements in a list. I wrote a version
with an accumulator and made an attempt to write an equivalent
predicate using difference lists. I was motivated to use difference
lists to learn them better and to avoid having to reverse the list at
the end of the computation.

The difficulty is that I use member/2 to decide if I can safely discard
an element by testing if it is already part of the output. With
difference lists, the member/2 predicate will find a variable in the
difference list and bind to it. This causes the evaluation to fail.

Below is some sample code that demonstrates what I am trying to explain
here.

Thanks for any suggestions.

John

% uniq_acc works
uniq_acc(List,Set) :- uniq_acc(List,[],Set1), reverse(Set1, Set).
uniq_acc([],A,A).
uniq_acc([Head|Tail],Acc,Result) :-
member(Head,Acc),
!,
uniq_acc(Tail,Acc,Result).
uniq_acc([Head|Tail],Acc,Result) :-
uniq_acc(Tail,[Head|Acc],Result).

% uniq_dl fails in the call to member below
uniq_dl(List,Set) :- uniq_dl(List,Set,[]).
uniq_dl([],Slot,Slot).
uniq_dl([Head|Tail],Set,Slot) :-
member(Head,Set), % trouble is here.
!,
uniq_dl(Tail,Set,Slot).
uniq_dl([Head|Tail],[Head|Slot1],Slot) :-
uniq_dl(Tail,Slot1,Slot).

.



Relevant Pages

  • Re: An instance of Russells paradox?
    ... >>infinite generalization of the arity of every predicate (I got this ... the representation ... > of lists is actually syntactic sugar for the binary term ... notation, the list notation, and the operator notation. ...
    (sci.logic)
  • Re: backtracking in prolog
    ... The predicate is called foo for example (so foo takes two ... that does not satisfy the given predicate. ... backtracking, ... expect to happen for various lists that have more than ...
    (comp.lang.prolog)
  • Re: backtracking in prolog
    ... The predicate is called foo for example (so foo takes two ... that does not satisfy the given predicate. ... backtracking, ... expect to happen for various lists that have more than ...
    (comp.lang.prolog)
  • Re: backtracking in prolog
    ... Con is just another bunch of rules I have in the database ... The predicate is called foo for example (so foo takes two ... backtracking, ... expect to happen for various lists that have more than ...
    (comp.lang.prolog)
  • Re: backtracking in prolog
    ... The predicate is called foo for example (so foo takes two ... that does not satisfy the given predicate. ... backtracking, ... expect to happen for various lists that have more than ...
    (comp.lang.prolog)