我的解决方案假设空模板意味着无限多次重复:
reps(L, [], inf):-
is_proper_list(L).
reps(L, Template, N):-
dif(Template, []),
reps(L, Template, 0, N).
reps([], _, N, N).
reps(L, Template, N, N2):-
starts_with(Template, L, L1),
dif(N, N2),
succ(N, N1),
reps(L1, Template, N1, N2).
reps([A|L], Template, N, N1):-
split_l(Template, [A|L], H, C),
reps1(C, L, Template, H, N, N1).
reps1(y, L, Template, H, N, N1):-
dif(Template, H),
reps(L, Template, N, N1).
reps1(n, _, _, _, N, N).
starts_with([], L, L).
starts_with([A|T], [A|L], L1):-
starts_with(T, L, L1).
split_l([], _, [], y).
split_l([_|_], [], [], n).
split_l([_|T], [A|L], [A|L1], C):-
split_l(T, L, L1, C).
is_proper_list(L):-
freeze(L, is_proper_list1(L)).
is_proper_list1([]).
is_proper_list1([_|L]):- acyclic_term(L), is_proper_list(L).
通过一些示例运行:
?- reps([a,b,c,a,b,c],[a,b,c],N).
N = 2 ;
false.
?- reps([a,b,c,a,b,c],X,2).
X = [a] ;
X = [a, b] ;
X = [a, b, c] ;
X = [b] ;
X = [c] ;
X = [b, c] ;
false.
?- reps([a,B],[B],N).
B = a,
N = 2 ;
N = 1,
dif(B, a) ;
;
false.
?- reps([A,B,C],[D,E],N).
A = D,
B = E,
N = 1 ;
B = D,
C = E,
N = 1,
dif(f(D, E), f(A, D)) ;
;
N = 0,
dif(f(D, E), f(A, B)),
dif(f(D, C), f(B, E)) ;
;
false.
?- reps(L,T,N).
T = [],
N = inf,
freeze(L, is_proper_list1(L)) ;
;
L = [],
N = 0,
dif(T, []) ;
;
L = T, T = [_1740],
N = 1 ;
L = [_1740, _1740],
T = [_1740],
N = 2 ;
L = [_1740, _1740, _1740],
T = [_1740],
N = 3 .
...
一些更多的样例:
?- reps([], [], N).
N = inf ;
false.
?- reps(L, [], N).
N = inf,
freeze(L, is_proper_list1(L)) ;
;
false.
?- reps(L, [], N), L=[].
L = [],
N = inf ;
false.
list_n_reps([], 3, [])
?0次重复怎么样:list_n_reps(_, 0, [])
?如果所有参数都是变量,枚举的顺序如何:list_n_reps(L, N, R)
?然后你的解决方案可能是:L = R, R = [], N = 0 ; L = R, R = [], N = 1 ; ...
。这可以接受吗? - TA_intern