Skip to content

Instantly share code, notes, and snippets.

@mariapacana
Created August 23, 2013 03:41
Show Gist options
  • Save mariapacana/6315329 to your computer and use it in GitHub Desktop.
Save mariapacana/6315329 to your computer and use it in GitHub Desktop.
Prolog code for generating pairs.
use_module(library(clpfd)).
:- use_module(library(lists)).
ingroup(1, 1).
ingroup(2, 1).
ingroup(3, 1).
ingroup(5, 1).
ingroup(3, 2).
ingroup(4, 2).
ingroup(5, 2).
ingroup(6, 2).
ingroup(7, 2).
pair(P, Q, Prev) :- ingroup(P, G), ingroup(Q, G), P > Q,
\+member([P,Q],Prev).
all_pairs_([[P, Q] | R], Acc) :-
pair(P, Q, Acc),
Acc_ = [[P, Q] | Acc],
all_pairs(R, Acc_).
all_pairs_([[P]], Prev) :- ingroup(P, _).
all_pairs_([], Prev).
all_pairs(L, Prev) :- all_pairs_(L, Prev), flatten(L, M),
all_different(M).
groupings(S1, S2, Prev, Acc) :-
copy_term(S1, S2),
all_pairs(S1, Prev),
append(S1, Prev, Acc).
% S1 = [[A, B], [C, D], [E, F], [G]], groupings(S1, S2, [], Prev),
groupings(S2, S3, Prev, Prev_), groupings(S3, S4, Prev_, Prev__).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment