On Tuesday 13 May 2008 20:17:53 Chris Kaltwasser wrote:
> Hello,
>
> I am curious whether there is an easier way to find duplicate fact
> instances along with the "duplication count" than the one I have come up
> with.
>
> I have a fairly simple solution, but feel there should be something less
> "convoluted" than my solution.
>
> An example, I think best explains this:
>
> foo(a,aa).
> foo(a,ac).
> foo(a,aa).
> foo(b,aa).
> foo(b,xy).
> foo(b,xy).
> foo(b,xy).
> foo(b,ac).
> foo(b,aa).
>
> foo(X,Y,1) :- foo(X,Y).
>
> dupfoo(X,Y,C) :-
> bagof(Z,foo(X,Y,Z),L), length(L,C), C>1.
>
> The goal is to locate the cases where foo(X,Y) produces multiple identical
> solutions.
>
> My definition for dupfoo(X,Y,C) shown above gives the correct results on
> backtracking. Namely:
>
> ?- dupfoo(X,Y,C).
> X = a,
> Y = aa,
> C = 2 ;
> X = b,
> Y = aa,
> C = 2 ;
> X = b,
> Y = xy,
> C = 3.
>
> What I don't like, in particular, is that I had to create the foo/3
> predicate whose only function is to assist in collecting the duplicate
> count (C). It seems to me that there should be some way to make use of the
> bagof predicate without having to make use of the foo/3 predicate.
bagof(1, foo(X,Y), L), length(L,C), C>1.
Nobody ever said the template must contain variables from the goal :-)
Intuitively I would have choosen something along these lines:
findall(X-Y, foo(X, Y), Solutions),
msort(Solutions, Sorted),
<find duplicates in sorted list>
But this is a lot more typing and probably only a bit more efficient.
Cheers --- Jan
------------
For further info, please visit
http://www.swi-prolog.org/To unsubscribe, send a plaintext mail with "unsubscribe prolog <e-mail>"
in its body to
majordomo@...