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: pythonic filter function?
    ... ((predicate (car list)) ... (filter predicate (cdr list))))) ... I would like to note that the very best thing to do is to avoid writing code that conses lists. ... you should use standard parts of Common Lisp that do the consing for you. ...
    (comp.lang.scheme)
  • Re: Prolog syntax
    ... is the notation for lists, so is a list of length one containing only the variable TYPE. ... decomposes a term into a list with its first element the predicate name followed by the predicate's arguments. ... in your example TYPE contains the predicate name to be called, so Cat becomes the predicate starting with TYPE and a single parameter SS, in the last line "Cat.", the just built predicate is called. ... is an infix defined function symbol and originally used to build arithmetic expressions. ...
    (comp.lang.prolog)
  • Re: Applying Godel
    ... Godel sentence for 1 particular theory T, ... The proof predicate needs to be binary and primitive recursive. ... you have to encode Both sentences AND lists-of-sentences ...
    (sci.logic)
  • Re: xsl -- using math in element subscripts?
    ... What you call a subscript is actually not. ... It is a predicate. ... merging preserves the doc order and does not duplicate any nodes in both lists. ... Notice that the name of the age group on the first vehicle ...
    (comp.text.xml)