I have a problem,
I have a list with numeric elements such as in the example.
I´d like to find all pairs, and count it. (Every Element can only be one part of one pair)
?- num_pairs([4,1,1,1,4],N).
N=1;
Can anyone help me to solve this problem??
You need several things to make it work:
Here is how you can count:
Deletion can be done with SWI’s
delete/3predicate; this is a built predicate.Adding one conditionally requires two rules – one when the count equals one, and another one for when the count does not equal one.
Finally, counting pairs could look like this:
An empty list has no pairs; when an item is counted as part of a pair, its copies are removed from the rest of the list.
Here is this running program on ideone.