Prolog , , imho, .
, :
free(ann, 08:00 > 09:00).
free(ann, 10:00 > 11:00).
free(bob, 07:00 > 08:30).
free(bob, 10:00 > 11:00).
free(carla, 08:00 > 09:00).
free(carla, 10:00 > 10:15).
meetone(Person, Slot) :- free(Person, SlotP), contains(SlotP, Slot).
contains(Slot1, Slot2) :-
timepoints(Slot1, B1, E1),
timepoints(Slot2, B2, E2),
B1 =< E2, E1 >= B2.
timepoints(BH:BM > EH:EM, B, E) :-
B is BH*60 + BM,
E is EH*60 + EM.
main :- setof(Person, meetone(Person, 7:15 > 7:20), Available),
maplist(writeln, Available).
, : /2 timepoints/3.
...
,
?- main.
bob
true.
:
, ,
common_timeslot/3, (), :
common_timeslot(S1, S2, Common) :-
timepoints(S1, B1, E1),
timepoints(S2, B2, E2),
% do you mean intersection by common ?
...
common_timeslot(S, S, S).
,
main :-
setof(Sc/P/Q, Sp^Sq^(
free(P, Sp), free(Q, Sq), Q \= P,
common_timeslot(Sp, Sq, Sc)
), Commons),
maplist(writeln, Commons).
?- main.
(8:0>9:0)/ann/carla
(8:0>9:0)/carla/ann
(10:0>11:0)/ann/bob
(10:0>11:0)/bob/ann
true.
mat,
free(ann, 08:00 < 09:00).
free(ann, 10:00 < 11:00).
free(bob, 07:00 < 08:30).
free(bob, 10:00 < 11:00).
free(carla, 08:00 < 09:00).
free(carla, 10:00 < 10:15).
meetone(Person, Slot) :- free(Person, SlotP), contains(SlotP, Slot).
contains(Slot1, Slot2) :-
timepoints(Slot1, B1, E1),
timepoints(Slot2, B2, E2),
B1 =< E2, E1 >= B2.
timepoints(BH:BM < EH:EM, B, E) :-
( ( var(B), var(E) )
-> B is BH * 60 + BM,
E is EH * 60 + EM
; BH is B
BM is floor(B mod 60),
EH is E
EM is floor(E mod 60)
).
% common_timeslot(S, S, S).
common_timeslot(S1,S2,S) :-
timepoints(S1,B1,E1),
timepoints(S2,B2,E2),
B is max(B1,B2),
E is min(E1,E2),
B < E,
timepoints(S,B,E).
% base case: C passed all commonality test
check_common(C, [], C).
% S is the current common, to be checked for availability on person P
check_common(S, [P|Ps], C) :-
free(P, Sp),
common_timeslot(S, Sp, Ct),
check_common(Ct, Ps, C).
main :- setof(P, S^free(P,S), [FirstP|Others]),
forall(free(FirstP, S), (
check_common(S, Others, C),
writeln(FirstP:C)
)).
?- main.
ann: (8:0<8:30)
ann: (10:0<10:15)
true.
, /3 "". common_timeslot/3, .
, , "" . , forall/2, setof/3, , , Prolog.